AnilJ
AnilJ

Reputation: 2121

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"
    }
  }
}

Upvotes: 10

Views: 35875

Answers (2)

Pavel Novikov
Pavel Novikov

Reputation: 61

For example url for your file is: resources/__files/stubResponses/provider/mark/qu/mappings/response_200.json

You have to write in usingFilesUnderDirectory(): src/intTest/resources/__files/stubResponses/provider/mark/qu

Directory 'mappings' is very important.

Upvotes: 3

AnilJ
AnilJ

Reputation: 2121

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"
}

Upvotes: 13

Related Questions