TyrionWebDev
TyrionWebDev

Reputation: 361

Using WireMock how can we verify that a request of a given url has NOT been made?

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")

Upvotes: 36

Views: 40000

Answers (2)

Oleg Nenashev
Oleg Nenashev

Reputation: 2291

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

Upvotes: 0

best wishes
best wishes

Reputation: 6634

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

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

Upvotes: 46

Related Questions