我有春季啓動應用程序,目前我的所有測試都運行綠色,如果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 {
..
}
您使用的是什麼版本的Spring Boot? –
'TestCase'從哪裏來?如果它來自JUnit 3.8,那麼您應該立即刪除'extends TestCase',因爲您實際上正在使用JUnit 4.x. –
@ SamBrannen Sam我使用的是spring/boot-starter-parent工件版本1.3.5.RELEASE。在你的評論之後,我已經刪除了擴展的TestCase指令,但是我仍然得到那個綁定異常,即已經在使用的地址。你有什麼想法可以嘗試改變嗎? – Tito