Kong / unirest-java

Unirest in Java: Simplified, lightweight HTTP client library.
http://kong.github.io/unirest-java/
MIT License
2.58k stars 591 forks source link

MockClient: allow fine grained request verification or "un-expected" routes #504

Closed JamesBoon closed 7 months ago

JamesBoon commented 7 months ago

Is your feature request related to a problem? Please describe. I am using the MockClient in integration tests and have to mock multiple routes that are not part of the actual test. They should not be part of the verification when calling mockClient.verifyAll().
Using mockClient.defaultResponse() is not an option, as different calls return different responses.

Describe the solution you'd like Allow to mock routes like mockClient.expect(...) but do not include them in mockClient.verifyAll().
Something like:

// required by tested service
mockClient.route(HttpMethod.POST, "http://localhost/auth")
        .thenReturn(...)
        .withStatus(200);

// only this will be part of "verifyAll"
mockClient.expect(HttpMethod.GET, "http://localhost/something")
        .thenReturn(...)
        .withStatus(200);

// ... call to tested service

mockClient.verifyAll();

Describe alternatives you've considered Allow to verify a specific request.
Something like:

Assert clientAssert = mockClient.expect(...)
        .thenReturn(...)
        .getAssert();

// ... call to tested service

clientAssert.verifyAll();
ryber commented 7 months ago

the good news is, these methods already exist, they just were not exposed on the interfaces. So I've exposed the singular "verify" method on both the Expectation and ExpectedResponse interfaces. See the commit tagged in this issue for some tests showing usage.

I just released 4.2.1 that has this change, it should show up in Maven Central in the next hour or so.

JamesBoon commented 7 months ago

Thanks, that works as expected :slightly_smiling_face: