首页 > 解决方案 > 如何在每次测试之前或之后让 Spring Cloud Contract 重置 WireMock

问题描述

我们正在编写一个 Spring Boot 应用程序并使用 Cloud Contract WireMock 支持来存根支持服务。我们的测试类是这样注释的:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@AutoConfigureWireMock(port = 0)
public class Tests...

这工作得很好,除了一件事:我们发现 Spring Cloud 似乎没有在测试之间重置 WireMock,特别是删除存根,因此测试没有被正确隔离。当然,您可以使用@Before包含 a 的方法自己完成此操作reset(),但我们想知道这是否是故意的。是否有我们忽略的选项或必须使用的附加注释?

毕竟,@BeforeClass如果总是执行重置,就不可能在一个方法中定义存根,所以我们想知道开箱即用的反对是什么?

标签: javaspringspring-cloudwiremock

解决方案


The WireMock server can be reset at any time, removing all stub mappings and deleting the request log. If you’re using either of the JUnit rules this will happen automatically at the start of every test case. However you can do it yourself via a call to WireMock.reset() in Java or sending a POST request with an empty body to http://<host>:<port>/__admin/reset.

To reset just the stub mappings leaving the request log intact send a DELETE to http://<host>:<port>/__admin/mappings.

Hope this is useful.


推荐阅读