6

I'm using Spring Boot with spring-cloud-contract-wiremock and com.github.tomakehurst.wiremock dependencies. My wiremock definitions are stored in json files. Like that:

directoryA/mappings/detail-mapping-123.json:

{
  "request" : {
    "urlPath" : "/detail/123",
    "method" : "GET"
  },
  "response" : {
    "status" : 200,
    "bodyFileName" : "detail.json",
    "headers" : {
      "Content-Type" : "application/json;charset=UTF-8"
    }
  }
}

directoryA/__files/detail.json:

{
  "id": "123",
  "name": "name-123"
}

directoryB/mappings/search-mapping-123.json:

{
  "request" : {
    "urlPath" : "/service/usa/search",
    "queryParameters" : {
      "query": {
        "equalTo": "123"
      }
    },
    "method" : "GET"
  },
  "response" : {
    "status" : 200,
    "bodyFileName" : "search-123.json",
    "headers" : {
      "Content-Type" : "application/json;charset=UTF-8"
    }
  }
}

directoryB/__files/search-123.json:

{
  "count": 1,
  "units": [
    {
      "name": "A123"
    }
  ]
}

I have standard JUnit test class which is annotated with:

@AutoConfigureWireMock(stubs = {"classpath:/directoryA/mappings", "classpath:/directoryB/mappings"},
        files = {"classpath:/directoryA", "classpath:/directoryB"},
        port = 18081)

This files looks like are recognized correctly by wiremock and all definitions are parsed correctly, but the problem is with assigning correct body file to request: When application try to execute request:

GET http://localhost:18081/service/usa/search?query=123 HTTP/1.1

Then I'm getting error:

java.lang.RuntimeException: java.io.FileNotFoundException: /home/my-project-dir/target/test-classes/directoryA/__files/search-123.json (Not found such file or directory) 

So... The problem is that wiremock search for file defined in bodyFileName part of mapping definition (directoryB/mappings/search-mapping-123.json) in directory directoryA instead of directoryB, from where mapping file was used. If there will be used

/home/my-project-dir/target/test-classes/directoryB/__files/search-123.json

then everything should work fine...

Does somebody had similar problem? I'm not sure if this is a bug in my configuration or in wiremock library.

Krzysiek
  • 615
  • 8
  • 19
  • 2
    no replies? @Krzysiek the question is too long, i suggest to close this one and make it shorter – Adam Siemion Sep 18 '18 at 20:09
  • Most of that question is the code, which is basically optimized for stackoverflow ;) Anyway, I'll try to refactor this question to highlight the idea of the problem. Thanks! – Krzysiek Sep 19 '18 at 13:56
  • 1
    @Krzysiek seems weird. I would try and debug the code execution which build this file path and see where it goes wrong. Also it might be a bug - You can check here https://github.com/spring-cloud/spring-cloud-contract if there is a test for it (I couldn't find), and if not you can add another one to try and prove whether its a bug. – yishaiz Sep 26 '18 at 08:07
  • 1
    @yishaiz As usuall, I didn't have enough time to check it deeply, but probably it will be the only way to resolve this issue. For now, I've just changed order and put all responses to single directory. Not clean, but working workaround... for a while... ;) – Krzysiek Sep 26 '18 at 11:39

1 Answers1

1

Try to exclude the "stubs" and "files" arguments from annotation @AutoConfigureWireMock, and put your mappings/files in src/test/resources, wiremock gets by default from these path