我有多个具有多个路径的 REST API。我目前正在使用 spring 的 Web 环境来设置 http 服务器,并使用 junit 测试这些 API。所以我使用一个包含以下注释的类并注入(inject)端口:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@ActiveProfiles(ConfigConstants.PROFILE_LOCAL)
public abstract class TestBase{
@Autowired
protected TestRestTemplate restTemplate;
@LocalServerPort
public int httpPort;
...
}
我的测试类继承了Testbase.class
并使用相同的端口。
设置:
- 测试库
- A 类扩展了 TestBase
- B 类扩展了 TestBase
- ...
在日志中我可以看到不止一个 Spring Server,因为 Spring ASCI Logo 多次出现。
有没有办法启动一次 WebEnvironment 并将其共享给我的所有测试?
问候 蒙蒂
最佳答案
Is there a way to start the WebEnvironment once and share those to all of my tests?
假设您没有在子类中提供更改上下文缓存键的额外配置,那么这种情况会自动发生。
详情可见Context Caching Spring 引用手册的部分。
问候,
Sam(Spring TestContext 框架的作者)
关于java - spring如何使用一个web环境进行多个测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47556152/