I am trying to unit test a class whose main function is to make a REST client call. To invoke the REST api I am making use of OkHttp3 library.
var apiClient = new OkHttpClient().newBuilder().build();
var request = new Request.Builder()
.url(myApiUrl)
.method("GET", null)
.addHeader("Content-Type", APPLICATION_JSON_VALUE)
.addHeader("x-env", "DEV")
.addHeader("apikey", "API-KEY")
.build();
Google took me to MockServer framework which seems to be exactly what I want. So inside my test case I set the following expectation:
mockServerClient.when(
request()
.withMethod("GET").withPath("myApyUrl")
.withHeaders(
header("Content-Type", APPLICATION_JSON_VALUE),
header("x-env", "DEV"),
header("apikey", "API-KEY")
)
).respond(
response()
.withStatusCode(200)
.withContentType(MediaType.APPLICATION_JSON)
.withBody("some response body")
);
When I run the test I get a failure with the expectation I set as the reason:
method matched
path matched
body matched
headers didn't match
This is followed by a no expectation for
which contains a lot more headers than the one I expected. This in my view is true as I only wanted to test for the headers I am explicitly set and of course the request will have more than that.
I am quite sure what I am trying to test is a normal scenario and thee reason for the failure is that somehow I am "too strict" forcing MockServer to test all headers rather than the one I set/expect.
Being very new with MockServerI just tried to google but I could not spot a clear way to solve my problem.
Thank you in advance for your inputs.