Search code examples
springintegration-testingresteasywiremock

Wiremock does not receive any requests


I am writing an integrationtest for a resource (Jax-rs with resteasy) which invokes a thirdparty request to the Trustpilot Api to get a review link for a product. In my tests I wanted to mock this call by using Wiremock and invoke the request by using resteasy Dispatcher. The code below shows my Setup (names etc are changed).

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { MyResourceTestContext.class })
public class MyResourceIntegrationTest {


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

    @Autowired
    private MyResource myResource;

    private Dispatcher dispatcher;
    private MockHttpResponse response;

    @Before
    public void setUp() throws Exception {
        dispatcher = MockDispatcherFactory.createDispatcher();
        dispatcher.getRegistry().addSingletonResource(myResource);
        response = new MockHttpResponse();
    }

    @Test
    public void testResourceShouldReturnRedirectToTrustpilotreview() throws Exception {

        URI apiRequestURI = UriBuilder
                .fromUri("https://api.trustpilot.com/v1/private/product-reviews/business-units"
                        + "/{businessUnitId}}/invitation-links")
                .build(BUSINESS_UNIT_ID);

        URI expectedRedirectURI = UriBuilder.fromUri("https://products.trustpilot.com/#evaluate/{reviewLinkId}")
                .build(REVIEW_LINK_ID);

        stubFor(post(apiRequestURI.toString())
                .willReturn(okJson("{\"reviewLinkId\":\"" + REVIEW_LINK_ID + "\","
                        + "\"reviewUrl\":\"" + expectedRedirectURI.toString() + "\"}")));

        MockHttpRequest request = MockHttpRequest.get("/myResource");
        dispatcher.invoke(request, response);

        WireMock.verify(1, postRequestedFor(urlEqualTo(apiRequestURI.toString())));
        Assert.assertEquals(HttpStatus.TEMPORARY_REDIRECT.value(), response.getStatus());
        Assert.assertEquals(expectedRedirectURI.toString(), response.getOutputHeaders().getFirst("Location"));

    }

But what I'm getting is (with my actual bid in the url):

com.github.tomakehurst.wiremock.client.VerificationException: Expected at least one request matching: {
      "url" : "https://api.trustpilot.com/v1/private/product-reviews/business-units/<bid>/invitation-links",
      "method" : "POST"
    }
    Requests received: [ ]

Stepping into the request with the Debugger I can say this request and other requests are actually executed successfully. But they are not recognized/mocked by Wiremock.

Is there something wrong with my Wiremock setup? Any Ideas? Thank you!


Solution

  • There are a couple of reasons why this isn't sending anything to WireMock at the moment:

    1. WireMock only accepts relative URLs when stubbing so passing the full URL including protocol and domain won't work. Try something equivalent to stubFor(post(urlPathEqualTo("/v1/private/product-reviews/business-units/<bid>/invitation-links") instead.
    2. There doesn't seem to be any way for your class under test to know which host/port WireMock is running on and no forward proxying set up so the code is presumably still trying to call the real Trustpilot API.

    To solve 2. you have a couple of options:

    1. Set up your class/code under test to use localhost as the destination host and the result of wireMockRule.port() as the port number, or wireMockRule.httpsPort() if you're on HTTPS.
    2. Use the new auto JVM proxy setup feature to proxy your app's calls through WireMock. This assumes you're on WireMock 2.31.0+ and that your app's HTTP client respects the JVM proxy system properties.