2016-12-21 35 views
1

我有大约8 终端到终端的测试类,扩展抽象 SpringContextLoadingTest类,它看起来像这样:摇篮正在执行测试非常慢,因为它增加了很多测试套件

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) 
public abstract class SpringContextLoadingTest extends AbstractTestNGSpringContextTests { 
} 

我有@SpringBootApplication注释的主应用程序类。因为我使用TestNG,我在一个组中有一些类(“通道A”),而另一些中有一些(“通道B”)。

我的gradle做任务运行过程中出现不同的群体:

task runChannelA(type: Test) { 
    forkEvery = 1 
    useTestNG() { 
     includeGroups "channel A" 
    } 
} 

没有“forEvery = 1”,运行过程中出现超过1次测试时,有繁忙的港口的问题。

由于下面这个简单的配置,我收到来自gradle这个任务执行更详细的输出:

tasks.withType(Test) { 
    testLogging.showStandardStreams = true 
} 

没有它,它会显得测试执行后一样,应用程序挂起2分钟,在关闭EntityManagerFactory,但是这个标志揭示了Gradle拿起它没有被要求的测试。对于每个测试,无论它在哪个组中,gradle正在记录:

Gradle Test Executor 22 STANDARD_OUT 
2016-12-21 17:10:00.115 INFO --- [ Test worker] .b.t.c.SpringBootTestContextBootstrapper : Neither @ContextConfiguration nor @ContextHierarchy found for test class [mypackage.OtherTest], using SpringBootContextLoader 
2016-12-21 17:10:00.141 INFO --- [ Test worker] o.s.t.c.support.AbstractContextLoader : Could not detect default resource locations for test class [mypackage.OtherTest]: no resource found for suffixes {-context.xml, Context.groovy}. 
2016-12-21 17:10:00.143 INFO --- [ Test worker] t.c.s.AnnotationConfigContextLoaderUtils : Could not detect default configuration classes for test class [mypackage.OtherTest]: DbCongestionExploratoryTest does not declare any static, non-private, non-final, nested classes annotated with @Configuration. 
2016-12-21 17:10:00.455 INFO --- [ Test worker] .b.t.c.SpringBootTestContextBootstrapper : Found @SpringBootConfiguration mypackage.Application for test class mypackage.OtherTest 
2016-12-21 17:10:00.466 INFO --- [ Test worker] .b.t.c.SpringBootTestContextBootstrapper : Using TestExecutionListeners: [or[email protected]9404cc4, org.springframework.test[email protected]46876feb, org.springframewor[email protected]dd46df5, org.springfra[email protected]49e2c374] 

而且它需要很多时间,因为我有很多其他测试。在IntelliJ中可以看到我想要执行的测试已经通过后发生这种情况。例如,我在25秒后看到测试已经通过,但是因为它正在执行在我的项目中以这种方式设置的所有其他测试所做的任何尝试,runChannelA需要3分钟以上。有趣的是,我可以在这个奇怪的行为中停止这个过程,而IntelliJ中的进度条只是填充到最后,而且没有任何事情发生,一切都是绿色的,很棒。

有人可以帮助我吗?

回答

1

我已经解决了我的问题,感谢回答这个问题 Reuse spring application context across junit test classes

因为当JUnit是做春天不知道,它永远缓存所有的内容和使用JVM关闭挂钩关闭它们。

这就是为什么对于不同的情况下,你需要不同的JVM,因为上下文只能去除关闭jvm时。

可惜事实证明,如果你用“forkEvery = 1” gradle这个任务,那么Spring是分叉的JVM 环境下,你在你的classpath中有,包括在任何当前你正在执行测试的未使用上下文。

我有许多不同的上下文,因为我试图加载每个测试只有最低要求的配置。

我已尽可能地通过上下文解决了我的问题分组测试。我已经扩展SpringContextLoadingTest来加载更多的东西。它现在涵盖了我的大部分测试。使用此上下文的测试运行在一个gradle任务中,without forkEvery = 1.测试我需要加载不同上下文的位置(因为我更改了某个属性或加载了不同版本的bean)我在另一个gradle任务中运行,this时间与forkEvery = 1指定。