spring 在每个春季启动 @Test 上覆盖单个 @Configuration 类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39041542/
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-09-08 01:11:07  来源:igfitidea点击:

Override a single @Configuration class on every spring boot @Test

springspring-bootspring-test

提问by NotGaeL

On my spring boot application I want to override just one of my @Configurationclasses with a test configuration (in particular my @EnableAuthorizationServer@Configurationclass), on all of my tests.

在我的 Spring Boot 应用程序中,我想在所有@Configuration测试中仅用测试配置覆盖我的一个类(特别是我的@EnableAuthorizationServer@Configuration类)。

So far after an overview of spring boot testing featuresand spring integration testing featuresno straightforward solution has surfaced:

到目前为止,在对Spring Boot 测试特性Spring 集成测试特性进行了概述之后,没有直接的解决方案浮出水面:

  • @TestConfiguration: It's for extending, not overriding;
  • @ContextConfiguration(classes=…?)and @SpringApplicationConfiguration(classes =…?)let me override the whole config, not just the one class;
  • An inner @Configurationclass inside a @Testis suggested to override the default configuration, but no example is provided;
  • @TestConfiguration:用于扩展,而不是覆盖;
  • @ContextConfiguration(classes=…?)@SpringApplicationConfiguration(classes =…?)让我重写整个配置,而不仅仅是一个类;
  • 建议@Configuration在 a 内部使用内部类@Test覆盖默认配置,但没有提供示例;

Any suggestions?

有什么建议?

回答by alexbt

Inner test configuration

内测配置

Example of an inner @Configuration for your test:

测试的内部 @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
    }
}

Reusable test configuration

可重用的测试配置

If you wish to reuse the Test Configuration for multiple tests, you may define a standalone Configuration class with a Spring Profile @Profile("test"). Then, have your test class activate the profile with @ActiveProfiles("test"). See complete code:

如果您希望为多个测试重用测试配置,您可以使用 Spring Profile 定义一个独立的配置类@Profile("test")。然后,让您的测试类使用@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();
    }
}

@Primary

@基本的

The @Primaryannotation on the bean definition is to ensure that this one will have priority if more than one are found.

@Primary对bean定义标注为确保这一具有优先权如果不止一个被发现。

回答by Slava Babin

You should use spring boot profiles:

您应该使用spring 引导配置文件

  1. Annotate your test configuration with @Profile("test").
  2. Annotate your production configuration with @Profile("production").
  3. Set production profile in your properties file: spring.profiles.active=production.
  4. Set test profile in your test class with @Profile("test").
  1. 使用@Profile("test").
  2. 使用@Profile("production").
  3. 在您的属性文件中设置生产配置文件:spring.profiles.active=production.
  4. 在您的测试类中设置测试配置文件@Profile("test")

So when your application starts it will use "production" class and when test stars it will use "test" class.

因此,当您的应用程序启动时,它将使用“生产”类,而当测试开始时,它将使用“测试”类。

If you use inner/nested @Configurationclass it will be be used instead of a your application's primary configuration.

如果您使用内部/嵌套@Configuration类,它将被用来代替您的应用程序的主要配置。

回答by Tamas Hegedus

I recently had to create a dev version of our application, that should run with devactive profile out of the box without any command line args. I solved it with adding this one class as a new entry, which sets the active profile programmatically:

我最近不得不为我们的应用程序创建一个开发版本,它应该dev在没有任何命令行参数的情况下使用开箱即用的活动配置文件运行。我通过将这一类添加为新条目来解决它,它以编程方式设置活动配置文件:

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);
    }
}

See Spring Boot Profiles Example by Arvind Raifor more details.

有关更多详细信息,请参阅Arvind Rai 的 Spring Boot 配置文件示例