我有 spring boot 应用程序,如果 ai 单独运行它们,即手动运行,目前我所有的测试都运行绿色,但是当我运行 maven package 命令时,所有测试都以串行模式运行,即一个接一个。然后发生的是我得到:
"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("Development") 公共类 TomcatEmbededDevelopmentTesting1Profile 扩展 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 {
..
}