Java Gradle执行测试的速度非常慢,因为它正在向套件中添加许多测试
我有大约8个端到端测试类,它们扩展了我的摘要SpringContextLoadingTest类,如下所示:Java Gradle执行测试的速度非常慢,因为它正在向套件中添加许多测试,java,intellij-idea,gradle,spring-boot,testng,Java,Intellij Idea,Gradle,Spring Boot,Testng,我有大约8个端到端测试类,它们扩展了我的摘要SpringContextLoadingTest类,如下所示: @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) public abstract class SpringContextLoadingTest extends AbstractTestNGSpringContextTests { } 我有一个带有@SpringBootApplicati
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public abstract class SpringContextLoadingTest extends AbstractTestNGSpringContextTests {
}
我有一个带有@SpringBootApplication注释的主应用程序类
当我使用TestNG时,我在一个组中有一些类(“通道A”),在另一个组中有一些类(“通道B”)
我为不同的小组做了梯度任务:
task runChannelA(type: Test) {
forkEvery = 1
useTestNG() {
includeGroups "channel A"
}
}
如果没有“forEvery=1”,则在运行多个测试时,繁忙端口会出现问题
由于下面这个简单的配置,我从gradle任务执行中收到了更详细的输出:
tasks.withType(Test) {
testLogging.showStandardStreams = true
}
如果没有它,看起来就像在测试执行之后,应用程序在关闭EntityManagerFactory时会挂起2分钟,但是这个标志显示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: [org.springframework.test.context.web.ServletTestExecutionListener@9404cc4, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@46876feb, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@dd46df5, org.springframework.test.context.support.DirtiesContextTestExecutionListener@49e2c374]
这需要很多时间,因为我还有很多其他的测试。这是在IntelliJ中看到我想要执行的测试已经通过之后发生的。例如,我在25秒后看到测试已经通过,但因为它在我的项目中以这种方式设置其他所有测试,所以runChannelA需要3分钟以上的时间。有趣的是,我可以在这个奇怪的行为中停止这个过程,IntelliJ中的进度条一直填充到最后,就像什么都没有发生一样,一切都是绿色的,很棒的
有人能帮我解决这个问题吗?由于回答了这个问题,我已经解决了我的问题- 因为Spring不知道JUnit何时完成,所以它会永久缓存所有上下文,并使用JVM关闭钩子关闭它们 这就是为什么对于不同的上下文,您需要不同的jvm,因为只有在关闭jvm时才会删除上下文 不幸的是,如果在gradle任务中使用“forkEvery=1”,那么Spring将为类路径中的每个上下文(包括当前执行的任何测试中未使用的上下文)派生jvm 我有很多不同的上下文,因为我试图为每个测试加载只需要最低限度的配置
我已经尽可能地通过上下文解决了分组测试的问题。我已经扩展了SpringContextLoadingTest以加载更多内容。它现在涵盖了我的大部分测试。使用此上下文的测试在一个gradle任务中运行,不使用forkEvery=1。需要加载不同上下文的测试(因为我更改了某些属性或加载了不同版本的bean),我在另一个gradle任务中运行,这次指定了forkEvery=1