question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Custom Request Matcher NPE when unmatched

See original GitHub issue

wiremock-standalone v2.2.1

When using a Custom RequestMatcher and an unmatched Request is encountered I am seeing the below Exception.

java.lang.NullPointerException
    at com.github.tomakehurst.wiremock.verification.Diff.toString(Diff.java:60)
    at com.github.tomakehurst.wiremock.client.VerificationException.<init>(VerificationException.java:64)
    at com.github.tomakehurst.wiremock.client.VerificationException.forSingleUnmatchedRequest(VerificationException.java:43)
    at com.github.tomakehurst.wiremock.client.VerificationException.forUnmatchedRequests(VerificationException.java:48)
    at com.github.tomakehurst.wiremock.junit.WireMockRule.checkForUnmatchedRequests(WireMockRule.java:87)
    at com.github.tomakehurst.wiremock.junit.WireMockRule.access$000(WireMockRule.java:33)
    at com.github.tomakehurst.wiremock.junit.WireMockRule$1.evaluate(WireMockRule.java:73)
    at org.junit.rules.RunRules.evaluate(RunRules.java:20)

Looks like the documentation may be a bit out of date regarding custom matchers, so this could be an issue with the way I have set them up.

I’ve created a sample test that demonstrates the issue.

import com.github.tomakehurst.wiremock.http.Request;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import com.github.tomakehurst.wiremock.matching.MatchResult;
import com.github.tomakehurst.wiremock.matching.RequestMatcher;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.junit.Rule;
import org.junit.Test;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.requestMatching;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;

public class WmBugTest {

    @Rule
    public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort());


    @Test
    public void wmBug() throws Exception {
        wireMockRule.stubFor(requestMatching(new CustomRequestMatcher())
                        .willReturn(aResponse()
                                    .withHeader("Content-Type", "text/plain")
                                    .withBody("Hi")));
        unmatchedGet();
    }

    private void unmatchedGet() throws Exception {
        HttpClient client = HttpClientBuilder.create().build();
        HttpGet request = new HttpGet("http://localhost:" + wireMockRule.port() + "/unmatched");
        request.addHeader("User-Agent", "foobar");
        HttpResponse response = client.execute(request);

        System.out.println("Response Code : " + response.getStatusLine().getStatusCode());
    }


    public static class CustomRequestMatcher extends RequestMatcher {
        @Override
        public String getExpected() {
            return "Some custom expectation";
        }

        @Override
        public MatchResult match(Request request) {
            if (request.getUrl().equals("foobar")) {
                return MatchResult.exactMatch();
            } else {
                return MatchResult.noMatch();
            }
        }

        @Override
        public String getName() {
            return "custom-request-matcher";
        }
    }
}

If this is not user-error, can anyone suggest a workaround just to have the WireMockRule not fail during (with this internal Exception) and instead fail with the unmatched call Exception?

I spent 45mins or so debugging through StubMapping, RequestPattern, NearMiss and Diff and didn’t really come to any meaningful conclusion about what is at fault (eg should the Diff class be null safe or is the contract that urlMatcher should always be non-null). Happy to work on a pull request if someone can push me in the right direction.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
tomakehurstcommented, Oct 25, 2016

Actually, don’t worry. I’m about to push a fix.

0reactions
marksieverscommented, Nov 4, 2016

Fix looks good on the demo test, thanks for that.

com.github.tomakehurst.wiremock.client.VerificationException: A request was unmatched by any stub mapping. Closest stub mapping was: (Request pattern had a custom matcher so no diff can be shown)
    at com.github.tomakehurst.wiremock.client.VerificationException.forSingleUnmatchedRequest(VerificationException.java:43)
    at com.github.tomakehurst.wiremock.client.VerificationException.forUnmatchedRequests(VerificationException.java:48)
    at com.github.tomakehurst.wiremock.junit.WireMockRule.checkForUnmatchedRequests(WireMockRule.java:87)
    at com.github.tomakehurst.wiremock.junit.WireMockRule.access$000(WireMockRule.java:33)
    at com.github.tomakehurst.wiremock.junit.WireMockRule$1.evaluate(WireMockRule.java:73)
    at org.junit.rules.RunRules.evaluate(RunRules.java:20)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Custom Request Matcher NPE when unmatched #517 - GitHub
When using a Custom RequestMatcher and an unmatched Request is encountered I am seeing the below Exception. java.lang.NullPointerException at ...
Read more >
com.github.tomakehurst.wiremock.client.VerificationException
In your code you are stubbing out a response and then verifying that a request has been made for that stub. You are...
Read more >
Java Program To Find Unmatched values From Two Lists
Overview: In this tutorial, We'll be learning about a java program how to compare two lists and find out the unmatched contents f....
Read more >
Request Matching - WireMock
Here's an example showing all attributes being matched using WireMock's in-built match operators. It is also possible to write custom matching logic if...
Read more >
How to Throw & Handle Scala Exceptions - Rollbar
Here's how to respond to and investigate the cause of exceptions in Scala: 1. Try/Catch → 2. Finally Clauses → 3. Custom Exceptions....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found