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?