2

I create a stub in Wiremock using this code:

        getWiremock().stubFor(get(urlMatching("/rs-queue/messages.*"))
            .willReturn(aResponse()
                .withStatus(200)
                .withHeader("Content-Type", "application/json")
                .withBody(fromObjectToString(queueMessageItem))
            ));

I want this stub to be removed/disabled after it was called one time. I did not find any example in Wiremock docs. Have an idea to create a new thread and run this code in it:

 RequestPatternBuilder requestPatternBuilder = postRequestedFor(urlEqualTo(url))
            .withRequestBody(new EqualToPattern(fromObjectToString(queueMessageItem)));

        Awaitility.waitAtMost(Duration.of(1, ChronoUnit.MINUTES))
            .pollInterval(Duration.of(3, ChronoUnit.SECONDS))
            .until(() -> {
                try {
                    verify(moreThanOrExactly(1), requestPatternBuilder);
                    getWiremock().removeStubMapping(stubMapping.getUuid());
                } catch (VerificationException exception) {
                    return false;
                }
                return true;
            });

The approach looks dirty and too complicated and I still believe it is possible to configure wiremock stub to be auto removed after calling it one time. Any ideas?

alexey kurbatov
  • 117
  • 4
  • 10

2 Answers2

2

You can use either .reset() or .resetToDefault() to remove added stubs. From the documentation:

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

If you’ve created some file based stub mappings to be loaded at startup and you don’t want these to disappear when you do a reset you can call WireMock.resetToDefault() instead

So, either use getWireMock().reset() or getWireMock().resetToDefault() depending on your use case.

Additionally, you can use .removeStub(). In this case, I'd recommend declaring your .stubFor() as a variable, so you can more easily remove it later.

agoff
  • 5,818
  • 1
  • 7
  • 20
  • hey, in my question I added a code snippet where I remove stub but I do not like the approach. I am looking for a nice and neat way to disable/remove the stub – alexey kurbatov Dec 24 '22 at 15:57
1

There's nothing out of the box that will delete a stub on request, but you can get it to work with Webhooks:

IMPORTANT webhook requests are made asynchronously - the stub will not necessarily have been deleted yet when your request returns a response.

import com.github.tomakehurst.wiremock.WireMockServer;
import org.wiremock.webhooks.Webhooks;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.UUID;

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static com.github.tomakehurst.wiremock.http.RequestMethod.DELETE;
import static org.wiremock.webhooks.Webhooks.webhook;

...

wireMockServer = new WireMockServer(wireMockConfig().extensions(Webhooks.class));

...

UUID stubId = UUID.randomUUID();
getWiremock().stubFor(get(urlMatching("/rs-queue/messages.*"))
    .withId(stubId)
    .withPostServeAction("webhook", webhook()
        .withMethod(DELETE)
        .withUrl(getWiremock().url("/__admin/mappings/" + stubId))
    )
    .willReturn(aResponse()
        .withStatus(200)
        .withHeader("Content-Type", "application/json")
        .withBody(fromObjectToString(queueMessageItem))
    ));
Robert Elliot
  • 1,372
  • 13
  • 20