29

Given the following unit test, I can easily test if a request of a particular url has been made. Is there a way to do the opposite, verify that a request of a particular url has NOT been made?

i.e. verify that a request was made:

stubFor(post(urlEqualTo("/login")));

webclient.submit(testLogin);

verify(postRequestedFor(urlMatching("/login")

What I'd like to do - Verify that a request was NOT made:

stubFor(post(urlEqualTo("/login")));

webclient.submit(testLogin);

verify(postRequestedFor(urlNotMatching("/login")
best wishes
  • 5,789
  • 1
  • 34
  • 59
TyrionWebDev
  • 291
  • 1
  • 3
  • 3

2 Answers2

38

verify(exactly(0), postRequestedFor(urlEqualTo("/login")));

Basically checking if exactly 0 calls were made. To read more about it. see here

best wishes
  • 5,789
  • 1
  • 34
  • 59
  • 18
    If you want to verify that no requests at all have been made: `verify(0, anyRequestFor(anyUrl()));` – MyKey_ Jun 06 '18 at 11:35
  • 8
    The above answer is correct, there is just a small typo, the method is `anyRequestedFor` instead of `anyRequestFor`. – Fede Jul 09 '19 at 07:26
0

There response above works. For more complex asserts, WireMock Admin REST API allows retrieving all requests in the journal - GET: /__admin/requests. In your unit tests, e.g. with a Native Java library JUnit rules or with a Testcontainers module, you can:

  1. Provision a fresh instance via a test rule, or do s DELETE: /__admin/requests request over the REST API
  2. Run whatever test logic you have
  3. Call GET: /__admin/requests REST API to retrieve the list of requests
  4. Process this list to see that what actually was received by WireMock and how it was matched
Oleg Nenashev
  • 2,271
  • 1
  • 14
  • 4