Wiremock will be your buddy in this case (http://wiremock.org/docs/getting-started/)
In your spring boot test class, setup wiremock rule:
@Value("${wireMockServer.port}")
private int wireMockPort;
private WireMockRule wireMockRule;
@Rule
public WireMockRule getWireMockRule() {
if (wireMockRule == null) {
wireMockRule = new WireMockRule(wireMockConfig().port(wireMockPort).notifier(new Slf4jNotifier(true)));
}
return wireMockRule;
}
And then just mock your API:
String url = UriComponentsBuilder
.fromUriString("XXXXX").buildAndExpand("pathParam1", "pathParamVal").toUriString();
stubFor(get(urlEqualTo(url))
.willReturn(aResponse()
.withHeader("Content-Type", MediaType.APPLICATION_JSON_VALUE)
.withHeader("Connection", "close")
.withBody(asJson(mockedResponseObject))));
If you want not to programmatically stub your requests and store them in json files , then
mention the mappings location with the property wireMock.mappingsUnderClasspath
And inside that directory, create a folder called mappings and put your mapping json files:
eg.
{
"request": {
"method": "GET",
"urlPattern": "/foo/myApi"
},
"response": {
"status": 200,
"headers": {
"Content-Type": "application/json"
},
"bodyFileName": "mockedResponse.json"
}
}
And keep the mockedResponse.json in a directory called __files (double underscore) inside wireMock.mappingsUnderClasspath