【发布时间】:2022-01-29 10:50:45
【问题描述】:
我想切换 SpringBootTest 中的 webEnvironment 配置,以支持在管道中运行测试(测试需要能够自行启动服务器)和本地(我想使用独立服务器进行更快的测试。
认为拥有一个我可以设置的配置文件将是一个很好的解决方案,但 SpringBootTest 似乎完全忽略了我在同一级别附加的任何配置文件,它在 Spring 的生命周期中获取配置文件是否为时过早?有没有更好的方法来做到这一点?
@Profile("myProfile")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) // Starts regardless of what @Profile is
public class MyClass{
...
}
E: 相关问题: SpringBootTest enable/disable webEnvironment based on input
E2:经过下面的一些讨论,我放弃了远程模式,不值得麻烦。
【问题讨论】:
-
JUnit 创建测试,而不是 Spring。 JUnit 不关心 Spring 注释。
-
尝试 \@ActiveProfiles 注释。
-
@ActiveProfiles 仅激活配置文件吗?如果我这样做了,我仍然无法获得想要的行为,即能够切换应用程序是否应该启动。
标签: java spring-boot-test spring-profiles