单个测试执行期间的多个spring上下文问题

vsnjm48y  于 2021-07-23  发布在  Java
关注(0)|答案(1)|浏览(298)

我有接口和2个实现,根据属性值示例化。

@Bean
    @ConditionalOnProperty(value = "stitching", havingValue = "false", matchIfMissing = true)
    public LastEventProvider noStitchingLastEventProvider() {
        return new NoStitchingLastEventProvider();
    }
    @Bean
    @ConditionalOnProperty(value = "stitching", havingValue = "true")
    public LastEventProvider withStitchingLastEventProvider() {
            return new WithStitchingLastEventProvider();
    }

有一些测试类使用 stitching=true ,并且有一些类使用 stitching=false 当我单独运行测试类时,没有问题。当我运行所有测试类时,一个测试方法失败。在失败测试的日志中,我看到了非常奇怪的行为:根据我的日志,在测试开始时,我看到测试方法使用第二个示例,而在测试结束时,它使用第一个示例!!!

context: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@2e1e02b8, started on Tue Feb 16 14:12:00 IST 2021, parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@237cd4e5
14:12:30.256 [test-map-updater-status--test-0-C-1] TRACE c.m.m.s.a.WithStitchingLastEventProvider bla-bla
.
.
context: springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@348d18a3, started on Tue Feb 16 14:09:51 IST 2021, parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@7da10b5b
14:12:33.212 [test-map-updater-status--test-0-C-1] TRACE c.m.m.s.a.NoStitchingLastEventProvider - a-bla

在使用lasteventprovider之前,我记录了applicationcontext对象,并看到在测试期间有两个不同的应用程序上下文!!这是我在使用spring的12年中看到的最奇怪的问题。有什么想法吗?spring boot版本是2.2.2.0版本。谢谢!

vshtjzan

vshtjzan1#

我没有解决这个问题,但我实现了解决方法:我用junit5@tag annotation(@tag(“withsitching”)和@tag(“nostitching”)标记了所有测试类),创建了2个maven概要文件,并在构建中作为单独的步骤运行测试。

相关问题