#2718·wiremock

NPE on WireMockServer.getServeEvents when using a StubRequestFilter

Author: netmikeyCreated May 15, 2024Updated Sep 16, 2026
Labelsbug

Proposal

When querying the ServeEvents on a WireMockServer instance using a ServeEventQuery like this:

List<ServeEvent> matchingServeEvents = wireMockServer
         .getServeEvents(ServeEventQuery.forStubMapping(myStubMapping))
         .getServeEvents();

we're getting the following NullPointerException:

java.lang.NullPointerException: Cannot invoke "com.github.tomakehurst.wiremock.stubbing.StubMapping.getId()" because the return value of "com.github.tomakehurst.wiremock.stubbing.ServeEvent.getStubMapping()" is null
	at com.github.tomakehurst.wiremock.admin.model.ServeEventQuery.lambda$filter$2(ServeEventQuery.java:99)
	at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:178)
	at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:179)
	at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1625)
	at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509)
	at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499)
	at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:921)
	at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
	at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:682)
	at com.github.tomakehurst.wiremock.admin.model.ServeEventQuery.filter(ServeEventQuery.java:102)
	at com.github.tomakehurst.wiremock.core.WireMockApp.getServeEvents(WireMockApp.java:393)
	at com.github.tomakehurst.wiremock.WireMockServer.getServeEvents(WireMockServer.java:420)

I've tracked the issue down to this predicate definition:

https://github.com/wiremock/wiremock/blob/83ae2df0182126a2cdb0f2a3eaf601d775125c2b/src/main/java/com/github/tomakehurst/wiremock/admin/model/ServeEventQuery.java#L95-L100

This fails when a serveEvent returns null for getStubMapping(). I was wondering when this can happen (since serveEvent.getWasMatched() seems to have matched). It turns out this is the case when using a StubRequestFilterV2 implementation. In our case, we have a StubRequestFilterV2 responding with custom logic for a given URL. Something like:

java
    @Override
    public RequestFilterAction filter(Request request, ServeEvent serveEvent) {
        if (request.getUrl().equals("/api/some/end-point")) {
            return RequestFilterAction.stopWith(ResponseDefinition.okForJson(Map.of(/* some content */)));
        }
        return RequestFilterAction.continueWith(request);
    }

Without going into detail why we implemented this particular stub using a StubRequestFilterV2, when this filter triggers, it creates a ServeEvent that has wasMatched set to true but has stubMapping still be null.

Adding a null-check to the predicate should fix the issue.

Reproduction steps

(see above)

References

No response