15

I have defined wireMock server as follows:-

    private WireMockServer wireMockServer;
        @Before
        public void preSetup() throws Exception {
          wireMockServer = new WireMockServer(56789);
          wireMockServer.start();
        };

        @Override
        @After
        public void tearDown() {
          wireMockServer.stop();
        }

        @Test
        public void testSendMatchingMessage() throws Exception {

          wireMockServer.stubFor(get(urlEqualTo("/orders/v1/ordersearch/"))
            .willReturn(aResponse().withStatus(200).withBody("<response>Some content</response>")));

       }

But whenever I am hitting the url something like below

http://0.0.0.0:56789/orders/v1/ordersearch/?field=address%2Cfinance%2Cshipping&limit=10&page=2&q=createdFrom.gt%7E2016-01-11T10%3A12%3A13

I am getting the below error:-

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
    <title>Error 404 NOT_FOUND</title>
    </head>
    <body><h2>HTTP ERROR 404</h2>
    <p>Problem accessing /__files/orders/v1/ordersearch/. Reason:
    <pre>    NOT_FOUND</pre></p><hr /><i><small>Powered by Jetty://</small></i><br/>                                                
    <br/>                                                
    <br/>                                                
    <br/>                                                
    <br/>                                                
    <br/>                                                
    <br/>                                                
    <br/>                                                
    <br/>     
    </body>
    </html>

Can some one let me know what I am doing wrong?

Sylhare
  • 5,907
  • 8
  • 64
  • 80
tuk
  • 5,941
  • 14
  • 79
  • 162

4 Answers4

27

As per Stubbing - Wiremock (the 1st in Google on "wiremockserver urlequalto"):

Note: you must use urlPathEqualTo or urlPathMatching to specify the path, as urlEqualTo or urlMatching will attempt to match the whole request URL, including the query parameters.

ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
2

For anyone who is trying to add Wiremock to an Android app and stumbles upon this question:

If you run your mocking after the network call is made, it won't work. This may seem obvious but I was tripped up on it.

When you run an Espresso test, by default the activity test rule launches the activity right away, and so the activity was firing and pulling it's config data before my mocking code was actually run. I was seeing the same error as OP.

The fix is to make your activity test rule not launch initially, then mock your data, and tell the activity to start once you've done all that.

AdamMc331
  • 16,492
  • 10
  • 71
  • 133
1

Just to add a different scenario which in my case resulted in the exact same problem:

make sure that nothing else is using the Wiremock port.

I had a Docker container running in the background which had a mapping to the same port that was being used by my Wiremock. This was easy to miss because no binding errors (or any other kind) were thrown by Wiremock when running my tests - it started normally and my test requests would just return 404.

João Matos
  • 6,102
  • 5
  • 41
  • 76
0

The one I usually use is urlPathMatching that you can get when importing:

import static com.github.tomakehurst.wiremock.client.WireMock.*;

So in your test, you would have your service of class MyService that would make a rest call to an external api "/orders/v1/ordersearch/" which is that call that will be mocked with the wiremock stubFor.

Then the mockServer.verify that the mockServer has made a get to the external api when the service service.sendToExternalUrl().

  private WireMockServer mockServer;
  private MyService service;

  @Before
  public void preSetup() {
    mockServer = new WireMockServer(56789);
    mockServer.start();
  }

  @After
  public void tearDown() {
    mockServer.stop();
  }

  @Test
  public void testSendMatchingMessage() {
    UrlPattern externalUrl = urlPathMatching("/orders/v1/ordersearch/");
    stubFor(get(externalUrl).willReturn(aResponse().withStatus(200)));

    service.sendToExternalUrl();

    mockServer.verify(1, getRequestedFor(externalUrl)
        .withRequestBody(containing(new JSONObject().put("orders", "results").toString())));
  }
Sylhare
  • 5,907
  • 8
  • 64
  • 80