2016-12-21 91 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指定。