Open bryant1410 opened 8 years ago
interesting however there is WireMock : https://github.com/tomakehurst/wiremock, Which can do exactly this and bit more i think :
stubFor(get(urlEqualTo("/my/resource"))
.withHeader("Accept", equalTo("text/xml"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "text/xml")
.withBody("<response>Some content</response>")));
Result result = myHttpServiceCallingObject.doSomething();
assertTrue(result.wasSuccessFul());
verify(postRequestedFor(urlMatching("/my/resource/[a-z0-9]+"))
.withRequestBody(matching(".*<message>1234</message>.*"))
.withHeader("Content-Type", notMatching("application/json")));
MockWebServer is also really cool and I use it allot but its power is in full and sequential control of the requests/responses
I've used Wiremock recently and found it be a decent tool. Although, I was using it a way that more resembled blackbox testing; arguably a special case (at least it seemed that way to me). I think that generally, people should see if MockWebServer is adequate for needs before using WireMock.
@peter-tackage I agree. MockWebServer with its default dispatcher could be enough for most for the cases and makes you think more about the way/sequence you make network requests which may help in discovering some unexpected side effects.
Regarding #114, we have been using RESTMock to mock Retrofit calls and it's working like a charm! It uses OkHttp's MockWebServer and it allows to define mock responses for matchers. For instance:
It also permits to verify if the calls have been done.