spring 如何使用一个网络环境进行多个测试

spring how to use one web-environment for multiple test

我得到了多个具有多个路径的 REST API。我目前正在使用 spring 的 Web 环境使用 junit 测试这些 APIS 以设置 http 服务器。所以我使用 class 包含以下注释并注入端口:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@ActiveProfiles(ConfigConstants.PROFILE_LOCAL)
public abstract class TestBase{

    @Autowired
    protected TestRestTemplate restTemplate;

    @LocalServerPort
    public int httpPort;

    ...
}

我的测试 classes 继承了 Testbase.class 并使用相同的端口。

设置:

在日志中,我可以看到不止一个 Spring 服务器,因为 Spring ASCI 徽标多次出现。

有没有办法启动一次 WebEnvironment 并将其共享给我的所有测试?

问候 蒙蒂

Is there a way to start the WebEnvironment once and share those to all of my tests?

假设您没有在子类中提供更改 上下文缓存键.

的额外配置,这会自动发生

详细信息可以在 Spring 参考手册的 Context Caching 部分找到。

此致,

Sam(Spring TestContext Framework 的作者)