Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wiremock error - there are no stub mappings in this WireMock instance

I have implemented a basic WireMock with a sample REST/HTTP request simulation. The server code implemented as below.

With this code, I get the following error when I issue the GET request from Postman (i.e. GET http://127.0.0.1:8089/some/thing).

No response could be served as there are no stub mappings in this WireMock instance.

What is missing in my setup/code?

import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;

public class MockApp {

    private WireMockServer wireMockServer;

    public MockApp(String testSpec) {
        wireMockServer = new WireMockServer(WireMockConfiguration.options().
                port(8089).
                usingFilesUnderDirectory(testSpec).
                disableRequestJournal());
    }

    public void start() {
        wireMockServer.start();
    }

    public void stop() {
        wireMockServer.stop();
    }
}

The main function is:

public class MockMain {

    public static void main(String[] args) {

        String baseDir = System.getProperty("user.dir");
        String testResource = baseDir + "/resources/testconfig/";

        MockAMS mockAMS = new MockAMS(testResource);

        mockAMS.start();
    }
}

Under 'resources/testconfig', there is a file called mapping.json containing:

{
  "request": {
    "method": "GET",
    "url": "/some/thing"
  },
  "response": {
    "status": 200,
    "body": "Hello world!",
    "headers": {
      "Content-Type": "text/plain"
    }
  }
}
like image 287
AnilJ Avatar asked Feb 16 '19 10:02

AnilJ


1 Answers

I found solution for this. So, basically we need create a folder called "mappings" (exact name) under the directory identified by "testResource" variable. So in above code example, the mapping.json file will be stored at location: "MockApp/resources/testconfig/mappings/mapping.json".

Once this, is done, it will print the following output. As can be seen in the logs, "Stub mapping size is 1". This will be printed once you add the following line in the code.

System.out.println("Stub mapping size: " + wireMockServer.getStubMappings().size());

Stub mapping size: 1
{
  "id" : "da5735a6-b6cc-45aa-8256-fb88b5670610",
  "request" : {
    "url" : "/some/thing",
    "method" : "GET"
  },
  "response" : {
    "status" : 200,
    "body" : "Hello world!",
    "headers" : {
      "Content-Type" : "text/plain"
    }
  },
  "uuid" : "da5735a6-b6cc-45aa-8256-fb88b5670610"
}
like image 63
AnilJ Avatar answered Sep 18 '22 12:09

AnilJ