@SpringBootTest 可切换的网络环境?

@SpringBootTest Toggable webEnvironment?

我想切换 SpringBootTest 中的 webEnvironment 配置以支持 运行 管道中的测试(测试需要能够自行启动服务器)和本地(我在这里想使用独立服务器进行更快的测试。

认为有一个我可以设置的配置文件是一个很好的解决方案,但是 SpringBootTest 似乎完全忽略了我在同一级别附加的任何配置文件,它在 [=20 中是否太早了=] 获取配置文件的生命周期?有更好的方法吗?

@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:经过下面的一些讨论,我放弃了远程模式,不值得麻烦。

我想知道你为什么在这里还要依赖spring? JUnit 已经有 @EnabledIf 注释 (documentation),因此您可以使用它甚至不尝试 运行 测试。

根据我的理解,它甚至更好,因为这将适用于所有可能甚至 运行 spring 的测试(例如单元测试)。此外,从性能端点来看应该更好(您甚至不要尝试 运行 应用程序上下文、find/register bean 等)