2016-06-14 148 views
1

我有春季啓動應用程序,目前我的所有測試都運行綠色,如果AI分別運行它們,即手動,但是當我運行Maven包命令,然後所有。在後對方即串行模式試運行,然後會發生什麼事是,我得到:SpringBoot junit測試多個TomcatServletContainerInitializer的 - BindException:地址已經在使用「

"java.net.BindException: Address already in use". 

基本上是因爲測試串行地運行他們都試圖聲稱插座127.0.0.1:8081而且,由於該插座處於 TIME_WAIT狀態,第二個測試無法聲明該插座。即netstat的輸出-apn

netstat的-apn | grep的-i 8080

tcp 0  0 127.0.0.1:8080   127.0.0.1:33952   TIME_WAIT 
  • 現在我有兩個不同的配置文件配置即開發和生產,即見下文:

    @Configuration @profile( 「發展」) public class TomcatEmbededDevelopmentTesting1Profile extends SpringServletContainerInitializer {...}

    @Configuration @Profile(「Production」)公共類 TomcatEmbededProductionProfile擴展 SpringServletContainerInitializer {..}

我尋找後,現在是指定哪些客戶TomcatServletContainerInitializer我可以運行一個特點,即我將擁有5種不同的一個都不同端口上偵聽/插座。 問題是如果我有5個不同的TomcatEmbeded配置類都標有「@Profile(」Development「)」,那麼我該如何告訴junit執行我需要的配置類。我已經厭倦了使用@SpringApplicationConfiguration,但那不會飛。

@SpringApplicationConfiguration(classes = { 
     ... 
     TomcatEmbededDevelopmentTesting1Profile.class, 
     ... 
     }) 

然後我發現,說明有一些神奇的註釋@IntegrationTest(「server.port:0」)網的一篇文章這是假設隨機端口,但沒有工作對我來說好。 什麼是在春季做正確的方法?任何提示都非常感謝。

這裏就是比如我測試的:

@RunWith(SpringJUnit4ClassRunner.class) 
@TestExecutionListeners(listeners={ ServletTestExecutionListener.class, 
            DependencyInjectionTestExecutionListener.class, 
            DirtiesContextTestExecutionListener.class, 
            WithSecurityContextTestExecutionListener.class 
            } 
     ) 

@SpringApplicationConfiguration(classes = { 
     SecurityWebApplicationInitializerDevelopment.class, 
     SecurityConfigDevelopment.class, 
     TomcatEmbededDevelopmentTesting1Profile.class, 
     Internationalization.class, 
     MVCConfigDevelopment.class, 
     PersistenceConfigDevelopment.class, 
     ServerAuthenticationSuccessHandler.class, 
     ServerRedirectAuthenticationSuccessHandler.class, 
     LogicServiceRegistryPostProcessor.class 
     }) 
@WebAppConfiguration 
@EnableWebSecurity 
@EnableWebSocket 
@EnableGlobalMethodSecurity(prePostEnabled = true) 
//@IntegrationTest({"server.port=0", "management.port=0"}) 
@ActiveProfiles("Development") 
    public class quickTest extends TestCase { 
.. 
} 
+0

您使用的是什麼版本的Spring Boot? –

+0

'TestCase'從哪裏來?如果它來自JUnit 3.8,那麼您應該立即刪除'extends TestCase',因爲您實際上正在使用JUnit 4.x. –

+0

@ SamBrannen Sam我使用的是spring/boot-starter-parent工件版本1.3.5.RELEASE。在你的評論之後,我已經刪除了擴展的TestCase指令,但是我仍然得到那個綁定異常,即已經在使用的地址。你有什麼想法可以嘗試改變嗎? – Tito

回答

3

對於初學者來說,@Enable*註解使用Spring引導1.3.x的支持測試類所以刪除它們並將它們移到實際的@Configuration類。其次,向@SpringApplicationConfiguration提供大量的課程列表是最糟糕的做法。最佳做法是爲集成測試定義一個@Configuration類,其中使用@Import來包含您需要的任何其他配置類。

三,使用@WebIntegrationTest(randomPort = true)代替@WebAppConfiguration

四,不擴展TestCase:這是JUnit 4,而不是JUnit 3。

五,不包括ServletTestExecutionListener:它僅用於外的容器集成測試,以及你正在做的是在容器集成測試(即在嵌入式Tomcat servlet容器)。

第六,如果您需要隨機端口號,只需通過@Value("${local.server.port}")注入它。

@RunWith(SpringJUnit4ClassRunner.class) 
@TestExecutionListeners(
    listeners = WithSecurityContextTestExecutionListener.class, 
    mergeMode = MERGE_WITH_DEFAULTS 
) 
@SpringApplicationConfiguration({ 
     SecurityWebApplicationInitializerDevelopment.class, 
     SecurityConfigDevelopment.class, 
     TomcatEmbededDevelopmentTesting1Profile.class, 
     Internationalization.class, 
     MVCConfigDevelopment.class, 
     PersistenceConfigDevelopment.class, 
     ServerAuthenticationSuccessHandler.class, 
     ServerRedirectAuthenticationSuccessHandler.class, 
     LogicServiceRegistryPostProcessor.class 
     }) 
@WebIntegrationTest(randomPort = true) 
@ActiveProfiles("Development") 
public class QuickTest { 

    @Value("${local.server.port}") 
    int port; 

    // ... 
} 

所以,看看有沒有什麼讓你任何進一步的,並在春季啓動的測試支持讀取Spring Boot Reference Manual進一步的細節。