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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With