[英]Java Spring Boot Test: How to exclude java configuration class from test context
I have a Java web app with spring boot我有一个带有 Spring Boot 的 Java Web 应用程序
When run test I need to exclude some Java config files:运行测试时,我需要排除一些 Java 配置文件:
Test config (need to include when test run):测试配置(测试运行时需要包含):
@TestConfiguration
@PropertySource("classpath:otp-test.properties")
public class TestOTPConfig { }
Production config (need to exclude when test run):生产配置(测试运行时需要排除):
@Configuration
@PropertySource("classpath:otp.properties")
public class OTPConfig { }
Test class (with explicit config class):测试类(带有显式配置类):
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestAMCApplicationConfig.class)
public class AuthUserServiceTest { .... }
Test config:测试配置:
@TestConfiguration
@Import({ TestDataSourceConfig.class, TestMailConfiguration.class, TestOTPConfig.class })
@TestPropertySource("classpath:amc-test.properties")
public class TestAMCApplicationConfig extends AMCApplicationConfig { }
Also have class:还有课:
@SpringBootApplication
public class AMCApplication { }
When test is running OTPConfig
used, but I need TestOTPConfig
...当测试运行
OTPConfig
使用OTPConfig
,但我需要TestOTPConfig
...
How can I do it?我该怎么做?
Typically you would use Spring profiles to either include or exclude Spring beans, depending on which profile is active.通常,您将使用 Spring 配置文件来包含或排除 Spring bean,具体取决于哪个配置文件处于活动状态。 In your situation you could define a production profile, which could be enabled by default;
在您的情况下,您可以定义一个生产配置文件,默认情况下可以启用该配置文件; and a test profile.
和测试配置文件。 In your production config class you would specify the production profile:
在您的生产配置类中,您将指定生产配置文件:
@Configuration
@PropertySource("classpath:otp.properties")
@Profile({ "production" })
public class OTPConfig {
}
The test config class would specify the test profile:测试配置类将指定测试配置文件:
@TestConfiguration
@Import({ TestDataSourceConfig.class, TestMailConfiguration.class, TestOTPConfig.class })
@TestPropertySource("classpath:amc-test.properties")
@Profile({ "test" })
public class TestAMCApplicationConfig extends AMCApplicationConfig {
}
Then, in your test class you should be able to say which profiles are active:然后,在您的测试类中,您应该能够说出哪些配置文件处于活动状态:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestAMCApplicationConfig.class)
@ActiveProfiles({ "test" })
public class AuthUserServiceTest {
....
}
When you run your project in production you would include "production" as a default active profile, by setting an environment variable:当您在生产中运行您的项目时,您将通过设置环境变量将“生产”作为默认活动配置文件:
JAVA_OPTS="-Dspring.profiles.active=production"
Of course your production startup script might use something else besides JAVA_OPTS to set the Java environment variables, but somehow you should set spring.profiles.active
.当然,您的生产启动脚本可能会使用除 JAVA_OPTS 之外的其他东西来设置 Java 环境变量,但您应该以某种方式设置
spring.profiles.active
。
You can also use @ConditionalOnProperty
like below:您还可以使用
@ConditionalOnProperty
如下所示:
@ConditionalOnProperty(value="otpConfig", havingValue="production")
@Configuration
@PropertySource("classpath:otp.properties")
public class OTPConfig { }
and for tests:和测试:
@ConditionalOnProperty(value="otpConfig", havingValue="test")
@Configuration
@PropertySource("classpath:otp-test.properties")
public class TestOTPConfig { }
Then specify in your main/resources/config/application.yml
然后在你的
main/resources/config/application.yml
otpConfig: production
and in your test/resources/config/application.yml
并在您的
test/resources/config/application.yml
otpConfig: test
You can also just mock the configuration you don't need.您也可以模拟不需要的配置。 For example:
例如:
@MockBean
private AnyConfiguration conf;
Put it into your test class.把它放到你的测试类中。 This should help to avoid that the real
AnyConfiguration
is being loaded.这应该有助于避免加载真正的
AnyConfiguration
。
Additionally, for excluding auto configuration:此外,为了排除自动配置:
@EnableAutoConfiguration(exclude=CassandraDataAutoConfiguration.class)
public class // ...
The easiest way that I use -我使用的最简单的方法 -
@ConditionalOnProperty
in my main source code.@ConditionalOnProperty
放在我的主要源代码中。 Voila!瞧!
You can use @ConditionalOnMissingClass("org.springframework.test.context.junit4.SpringJUnit4ClassRunner")
in Configuration class.您可以在 Configuration 类中使用
@ConditionalOnMissingClass("org.springframework.test.context.junit4.SpringJUnit4ClassRunner")
。 SpringJUnit4ClassRunner
can be replaced by any class which only exits in test environment. SpringJUnit4ClassRunner
可以被任何只存在于测试环境中的类替换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.