Reputation: 15413
When trying to test Client class, POST call stubbing works correctly, while GET isn't. What I'm doing wrong here / not understanding correctly?
Client code (POST):
HttpResponse httpResponse = new DefaultHttpRequestBuilder(HttpMethod.POST, SERVICE_URL_GET_MAGIC)
.withBody(parseMagic(magicName))
.execute();
With stubbing (POST):
stubFor(post(urlEqualTo("/api/get-magic"))
.withRequestBody(equalToJson(magicNameParsed))
.willReturn(aResponse()
.withHeader("Content-Type", "application/json")
.withBody(magicDtoParsed)));
Works correctly (httpResponse will have 200 OK).
When GET use, it won't stub the api call (httpResponse will be 404 Not found).
HttpResponse httpResponse = new DefaultHttpRequestBuilder(HttpMethod.GET, SERVICE_URL_GET_MAGIC)
.withBody(parseMagic(magicName))
.execute();
stubFor(get(urlEqualTo("/api/get-magic"))
.withRequestBody(equalToJson(magicNameParsed))
.willReturn(aResponse()
.withHeader("Content-Type", "application/json")
.withBody(magicDtoParsed)));
Upvotes: 1
Views: 6926
Reputation: 29907
I think the problem is that you're expecting a 'body' in your get request, but get requests cannot have a body (only PUT
and POST
requests can have a body).
try doing the following
stubFor(get(urlEqualTo("/api/get-magic"))
.willReturn(aResponse()
.withHeader("Content-Type", "application/json")
.withBody(magicDtoParsed)));
Note that I've removed the line .withRequestBody(equalToJson(magicNameParsed))
By the way. Stabbing is when you use a knife or sharp object to hurt someone/something. Stubbing is the word you want to use when talking in the context testing :)
Upvotes: 1