11

We are writing a Spring Boot application and use the Cloud Contract WireMock support to stub a backing service. Our test class is annotated like so:

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

This works fine except for one thing: We found out that Spring Cloud does not seem to reset WireMock, in particular delete stubs, in between tests so that tests are not isolated properly. Of course, you can accomplish this yourself with a @Before method containing a reset(), but we wonder whether this is intentional. Is there an option that we have overlooked or an additional annotation one has to use?

After all, it is not possible to define stubs in a @BeforeClass method that would be gone if a reset would always be performed, so we wonder what speaks against doing it out of the box?

arcimboldo
  • 121
  • 1
  • 1
  • 5

3 Answers3

7

Configure Spring Boot property:

wiremock:
  reset-mappings-after-each-test: true

ref: https://github.com/spring-cloud/spring-cloud-contract/commit/67119e62f6b30da56b06aade87ec3ba61de7fd24

michaldo
  • 4,195
  • 1
  • 39
  • 65
2

I ended up injecting WireMockServer and running wireMockServer.resetAll() in @BeforeEach.

bushwakko
  • 31
  • 7
  • 1
    Of course, we did the same thing, my question was more like: Is there an out-of-the-box solution? Anyway, the Spring Contract team acknowledged in the meantime that this is a bug and is fixed with 2.1.4.BUILD-SNAPSHOT and 2.2.0.BUILD-SNAPSHOT. – arcimboldo Oct 07 '19 at 11:22
1

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.

Mebin Joe
  • 2,172
  • 4
  • 16
  • 22
  • 2
    Thanks for your reply. Because we are using the WireMock support of Spring Cloud Contract, we do not use the WireMockRule. My question is: Why does Cloud Contract not reset WireMock before each test automatically? Is this intentional, and if so why? Or is it a involuntary omission, aka bug? – arcimboldo Mar 12 '19 at 08:25