在每个 spring 启动时覆盖单个 @Configuration class @Test
Override a single @Configuration class on every spring boot @Test
在我的 spring 引导应用程序上,我只想用测试配置覆盖我的 @Configuration
classes 之一(特别是我的 @EnableAuthorizationServer
@Configuration
class),在我所有的测试中。
到目前为止,在 spring boot testing features and spring integration testing features 的概述之后,还没有出现直接的解决方案:
@TestConfiguration
:用于扩展,而不是覆盖;
@ContextConfiguration(classes=…)
和 @SpringApplicationConfiguration(classes =…)
让我覆盖整个配置,而不仅仅是 class;
- 建议在
@Test
中使用内@Configuration
class来覆盖默认配置,但没有提供示例;
有什么建议吗?
内测配置
用于测试的内部@Configuration 示例:
@RunWith(SpringRunner.class)
@SpringBootTest
public class SomeTest {
@Configuration
static class ContextConfiguration {
@Bean
@Primary //may omit this if this is the only SomeBean defined/visible
public SomeBean someBean () {
return new SomeBean();
}
}
@Autowired
private SomeBean someBean;
@Test
public void testMethod() {
// test
}
}
可重复使用的测试配置
如果您希望为多个测试重复使用测试配置,您可以定义一个独立的配置 class 和 Spring 配置文件 @Profile("test")
。然后,让您的测试 class 使用 @ActiveProfiles("test")
激活配置文件。查看完整代码:
@RunWith(SpringRunner.class)
@SpringBootTests
@ActiveProfiles("test")
public class SomeTest {
@Autowired
private SomeBean someBean;
@Test
public void testMethod() {
// test
}
}
@Configuration
@Profile("test")
public class TestConfiguration {
@Bean
@Primary //may omit this if this is the only SomeBean defined/visible
public SomeBean someBean() {
return new SomeBean();
}
}
@小学
bean定义上的@Primary
注解是为了确保如果找到多个,这个优先。
你应该使用 spring boot profiles:
- 用
@Profile("test")
注释您的测试配置。
- 使用
@Profile("production")
注释您的生产配置。
- 在您的属性文件中设置生产配置文件:
spring.profiles.active=production
。
- 在您的测试 class 中设置测试配置文件
@Profile("test")
。
因此当您的应用程序启动时它将使用 "production" class 并且当测试星时它将使用 "test" class.
如果您使用 inner/nested @Configuration
class 它将被用来代替您的应用程序的主要配置。
我最近不得不创建我们应用程序的开发版本,它应该 运行 和 dev
开箱即用的活动配置文件,没有任何命令行参数。我通过添加这个 class 作为新条目解决了这个问题,它以编程方式设置活动配置文件:
package ...;
import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.Import;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.StandardEnvironment;
@Import(OriginalApplication.class)
public class DevelopmentApplication {
public static void main(String[] args) {
SpringApplication application =
new SpringApplication(DevelopmentApplication.class);
ConfigurableEnvironment environment = new StandardEnvironment();
environment.setActiveProfiles("dev");
application.setEnvironment(environment);
application.run(args);
}
}
有关详细信息,请参阅 Spring Boot Profiles Example by Arvind Rai。
在我的 spring 引导应用程序上,我只想用测试配置覆盖我的 @Configuration
classes 之一(特别是我的 @EnableAuthorizationServer
@Configuration
class),在我所有的测试中。
到目前为止,在 spring boot testing features and spring integration testing features 的概述之后,还没有出现直接的解决方案:
@TestConfiguration
:用于扩展,而不是覆盖;@ContextConfiguration(classes=…)
和@SpringApplicationConfiguration(classes =…)
让我覆盖整个配置,而不仅仅是 class;- 建议在
@Test
中使用内@Configuration
class来覆盖默认配置,但没有提供示例;
有什么建议吗?
内测配置
用于测试的内部@Configuration 示例:
@RunWith(SpringRunner.class)
@SpringBootTest
public class SomeTest {
@Configuration
static class ContextConfiguration {
@Bean
@Primary //may omit this if this is the only SomeBean defined/visible
public SomeBean someBean () {
return new SomeBean();
}
}
@Autowired
private SomeBean someBean;
@Test
public void testMethod() {
// test
}
}
可重复使用的测试配置
如果您希望为多个测试重复使用测试配置,您可以定义一个独立的配置 class 和 Spring 配置文件 @Profile("test")
。然后,让您的测试 class 使用 @ActiveProfiles("test")
激活配置文件。查看完整代码:
@RunWith(SpringRunner.class)
@SpringBootTests
@ActiveProfiles("test")
public class SomeTest {
@Autowired
private SomeBean someBean;
@Test
public void testMethod() {
// test
}
}
@Configuration
@Profile("test")
public class TestConfiguration {
@Bean
@Primary //may omit this if this is the only SomeBean defined/visible
public SomeBean someBean() {
return new SomeBean();
}
}
@小学
bean定义上的@Primary
注解是为了确保如果找到多个,这个优先。
你应该使用 spring boot profiles:
- 用
@Profile("test")
注释您的测试配置。 - 使用
@Profile("production")
注释您的生产配置。 - 在您的属性文件中设置生产配置文件:
spring.profiles.active=production
。 - 在您的测试 class 中设置测试配置文件
@Profile("test")
。
因此当您的应用程序启动时它将使用 "production" class 并且当测试星时它将使用 "test" class.
如果您使用 inner/nested @Configuration
class 它将被用来代替您的应用程序的主要配置。
我最近不得不创建我们应用程序的开发版本,它应该 运行 和 dev
开箱即用的活动配置文件,没有任何命令行参数。我通过添加这个 class 作为新条目解决了这个问题,它以编程方式设置活动配置文件:
package ...;
import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.Import;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.StandardEnvironment;
@Import(OriginalApplication.class)
public class DevelopmentApplication {
public static void main(String[] args) {
SpringApplication application =
new SpringApplication(DevelopmentApplication.class);
ConfigurableEnvironment environment = new StandardEnvironment();
environment.setActiveProfiles("dev");
application.setEnvironment(environment);
application.run(args);
}
}
有关详细信息,请参阅 Spring Boot Profiles Example by Arvind Rai。