NullPointerException in MockClient
sulakm opened this issue · 3 comments
Describe the bug
NullPointerException occures in MockClient if both path is body expectations are specified and path is not fullfilled.
A clear and concise description of what the bug is.
To Reproduce
MockClient mock = MockClient.register();
mock.expect(HttpMethod.POST, "myurl").body("test").thenReturn().withStatus(200);
mock.verifyAll();
Proposed fix
Please change the condition from expectedBody != null
to expectedBodyStatus != null
on the line in the link below:
It makes more sense to test the reference you are using for method invocation.
This reference is only set if the body is being evaluated.
if(expectedBodyStatus != null){ sb.append("Body:\n"); sb.append("\t" + expectedBodyStatus.getDescription());
Also it seems like the method in expect(HttpMethod, path) is ignored.
I am not sure if it is a bug or intended feature. It is either a method or a path but not both.
I have specified DELETE like this mockClient.expect(HttpMethod.DELETE, "some url").thenReturn().withStatus(200);
mockClient.verifyAll() was satisfied even though only PUT method was sent.
With more expect with different methods the first is used and the others cause UnirestAssertion, regardless of HttpMethod.
The cause is probably missing parentheses here:
return this.method.equals(request.getHttpMethod()) && (
this.path == null || this.path.equalsIgnoreCase(p.baseUrl()); )
this is resolved in the most recent releases
@ryber Many thanks for fixing null expectedBodyStatus.
Can you please also add parethenses after && operator on this line as sugested in comment above ? It is still missing in the latest revision.