简体   繁体   English

Java Spring Boot 测试:如何从测试上下文中排除 Java 配置类

[英]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 -我使用的最简单的方法 -

  1. Put @ConditionalOnProperty in my main source code.@ConditionalOnProperty放在我的主要源代码中。
  2. Then define a test bean and add it as a part of the test configuration.然后定义一个测试 bean 并将其添加为测试配置的一部分。 Either import that configuration in the main test class or any other way.在主测试类或任何其他方式中导入该配置。 This is only needed when you need to register a test bean.这仅在您需要注册测试 bean 时才需要。 If you do not need that bean then go to next step.如果您不需要该 bean,则转到下一步。
  3. Add a property in application.properties under src/test/resources to disable the use of configuration class that is present in the main folder.在 src/test/resources 下的 application.properties 中添加一个属性以禁用主文件夹中存在的配置类的使用。

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.

相关问题 Spring Boot 1.5.4:在单元测试中排除配置类 - Spring Boot 1.5.4: exclude configuration class in unit test 如何将可以访问属性的受保护配置添加到 Spring Boot Test 上下文? - How to add a protected configuration with access to properties to a Spring Boot Test context? 获取 java.lang.IllegalStateException:在 Spring 启动中执行测试 class 时检测到 Logback 配置错误 - Getting java.lang.IllegalStateException: Logback configuration error detected while executing Test class in Spring boot 有没有一种简便的方法可以在Spring Boot中使用Java注释从测试范围中排除类? - Is there an easy way to exclude classes from test coverage using Java Annotations in Spring Boot? 如何使用 Spring 引导在 Java 中测试 Dropbox 上传? - How to test a dropbox upload in Java with Spring Boot? Java,Spring Boot,Test,entityManagerFactory - Java, Spring Boot, Test, entityManagerFactory 如何在不启动整个上下文的情况下测试 Spring 中的@Configuration class? - How to test @Configuration class in Spring without starting up whole context? 如何使用 map 的依赖注入测试 Spring 启动应用程序配置 class? - How to test Spring boot application configuration class with dependency injection of map? Spring Boot 将配置类自动装配到 Junit 测试中 - Spring Boot autowire configuration class into Junit Test 从 Spring-Boot 测试中排除 elasticsearchTemplate - Exclude elasticsearchTemplate from Spring-Boot Test
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM