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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39729752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:24:24  来源:igfitidea点击:

Java Spring Boot Test: How to exclude java configuration class from test context

javaspringtestingspring-boot

提问by Sergey

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 OTPConfigused, but I need TestOTPConfig...

当测试运行时OTPConfig使用,但我需要TestOTPConfig......

How can I do it?

我该怎么做?

回答by David Miller

Typically you would use Spring profiles to either include or exclude Spring beans, depending on which profile is active. 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:

通常,您将使用 Spring 配置文件来包含或排除 Spring bean,具体取决于哪个配置文件处于活动状态。在您的情况下,您可以定义一个生产配置文件,默认情况下可以启用该配置文件;和测试配置文件。在您的生产配置类中,您将指定生产配置文件:

@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.

回答by Ondrej Bozek

You can also use @ConditionalOnPropertylike below:

你也可以@ConditionalOnProperty像下面这样使用:

@ConditionalOnProperty(value="otpConfig", havingValue="production")
@Configuration
@PropertySource("classpath:otp.properties")
public class OTPConfig { }

and for test:

和测试:

@ConditionalOnProperty(value="otpConfig", havingValue="test")
@Configuration
@PropertySource("classpath:otp-test.properties")
public class TestOTPConfig { }

Than 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

回答by u6707613

You can use @ConditionalOnMissingClass("org.springframework.test.context.junit4.SpringJUnit4ClassRunner")in Configuration class.SpringJUnit4ClassRunnercan be replaced by any class which only exits in test environment.

您可以@ConditionalOnMissingClass("org.springframework.test.context.junit4.SpringJUnit4ClassRunner")在 Configuration 类中使用。SpringJUnit4ClassRunner可以被任何只存在于测试环境中的类替换。

回答by Nishant

The easiest way that I use -

我使用的最简单的方法 -

  1. Put @ConditionalOnPropertyin my main source code.
  2. Then define a test bean and add it as a part of the test configuration. 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. If you do not need that bean then go to next step.
  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.
  1. 放入@ConditionalOnProperty我的主要源代码。
  2. 然后定义一个测试 bean 并将其添加为测试配置的一部分。在主测试类或任何其他方式中导入该配置。这仅在您需要注册测试 bean 时才需要。如果您不需要该 bean,则转到下一步。
  3. 在 src/test/resources 下的 application.properties 中添加一个属性以禁用主文件夹中存在的配置类的使用。

Voila!

瞧!