Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wiremock returning 404 for a stubbed url

Tags:

java

wiremock

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?

like image 460
tuk Avatar asked Mar 05 '16 20:03

tuk


People also ask

What is stubbing in WireMock?

Stubbing. A core feature of WireMock is the ability to return canned HTTP responses for requests matching criteria. These are described in detail in Request Matching.

How do I restart my WireMock?

The WireMock server can be reset at any time, removing all stub mappings and deleting the request log. If you're using either of the JUnit rules this will happen automatically at the start of every test case. However you can do it yourself via a call to WireMock.

What is the purpose of WireMock?

WireMock is a library for stubbing and mocking web services. It constructs an HTTP server that we can connect to as we would to an actual web service. When a WireMock server is in action, we can set up expectations, call the service and then verify its behaviors.

How do you throw an exception in WireMock?

Let's say your API returns BadRequest (http code 400) in case of exception UserNotFoundException: @PostMapping(value = "/test/user/get") public String myApi(@RequestParam String param1) { try {... } catch(UserNotFoundException e) { throw new ResponseStatusException(HttpStatus. BAD_REQUEST, "user not found");


1 Answers

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.

like image 107
ivan_pozdeev Avatar answered Sep 20 '22 12:09

ivan_pozdeev