在每个spring boot@测试中重写单个@Configuration类

在每个spring boot@测试中重写单个@Configuration类,spring,spring-boot,spring-test,Spring,Spring Boot,Spring Test,在我的spring boot应用程序上,我只想用测试配置覆盖我的@Configuration类中的一个(特别是我的@EnableAuthorizationServer@Configuration类),覆盖我的所有测试 到目前为止,在对以下问题进行概述之后,还没有直接的解决方案: @TestConfiguration:用于扩展,而不是覆盖 @ContextConfiguration(类=…​)和@SpringApplicationConfiguration(类=…​)让我覆盖整个配置,而不仅仅是

在我的spring boot应用程序上,我只想用测试配置覆盖我的
@Configuration
类中的一个(特别是我的
@EnableAuthorizationServer
@Configuration
类),覆盖我的所有测试

到目前为止,在对以下问题进行概述之后,还没有直接的解决方案:

  • @TestConfiguration
    :用于扩展,而不是覆盖
  • @ContextConfiguration(类=…​)
    @SpringApplicationConfiguration(类=…​)让我覆盖整个配置,而不仅仅是一个类
  • 建议使用
    @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
    }
}
可重用测试配置

如果您希望对多个测试重用测试配置,可以使用Spring概要文件
@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

bean定义上的
@Primary
注释是为了确保在找到多个bean时,该bean将具有优先级。

您应该使用:

  • @Profile(“test”)
    注释测试配置
  • 使用
    @Profile(“production”)
    注释您的生产配置
  • 在属性文件中设置生产配置文件:
    spring.profiles.active=production
  • 使用
    @profile(“test”)
    在测试类中设置测试配置文件
  • 所以,当应用程序启动时,它将使用“生产”类,当测试启动时,它将使用“测试”类


    如果您使用内部/嵌套的
    @Configuration
    类,它将被使用,而不是应用程序的主配置。

    我最近不得不创建一个应用程序的开发版本,该版本应该在没有任何命令行参数的情况下运行
    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);
        }
    }
    

    有关更多详细信息,请参阅。

    谢谢。我注意到,我还可以通过在
    src/test/java
    上删除覆盖的
    @EnableAuthorizationServer
    @Configuration
    类,在所有测试中覆盖该类。Spring引导规则:-)作为旁注,如果您只需要更改特定
    @配置
    上的一个值,而不是覆盖它,那么您可以使用测试类上的注释
    @ActiveProfiles({“test”,…})
    为您的测试启用特定的spring引导配置文件(例如名为
    test
    )(environment.getActiveProfiles())。包含(“测试”))
    在您的
    @配置上就可以了。如果其他bean中的一个在内部使用,您的解决方案将失败,它将由SomeBean类注入。为了使其工作,只需在SpringBootTest注释使用的类列表中添加ContextConfiguration类。即:@SpringBootTest(类={Application.class,SomeTest.ContextConfiguration.class})可重用部件不起作用。在我的示例中,尽管将@Primary放在测试配置中的bean上,Spring还是用主配置中的bean覆盖它。也就是说,它进行覆盖,但不是按照您期望的方式-选择了错误的bean。@Marksim是的,我也有同样的问题。如果您使用
    @TestConfiguration
    (而不是
    @Configuration
    )正如作者所提到的,bean得到了正确的选择。我更喜欢
    @Imort
    而不是
    @springbootest(classes=…)
    ,不知何故,在某些情况下,我得到了有线行为,其中一个带有额外
    @Import
    的测试工作,而另一个没有。用于测试的孔弹簧配置需要很多时间才能理解。puh