Okhttp: Move MockWebServer TestRule implementation

Created on 17 Jun 2016  ·  8Comments  ·  Source: square/okhttp

MockWebServer currently implements TestRule.
It is probably better to have a separate MockWebServerRule.
This would allow developers to use MockWebServer without the junit dependency.

Optionally, MockWebServerRule could be moved in to a mockwebserver-junit package for all junit-related functionality.

enhancement

Most helpful comment

Just to note that we use TestNG, so having MockWebServer pull in junit is a pain.

This doesn't have to wait until OkHttp 4 though. A new class MockWebServerLight could be added that did not have the dependency, and the pom.xml could be changed to declare the junit dependency as optional. Even if the pom.xml part was considered too risky, at least end users could exclude it in their pom.

All 8 comments

We can't do this until OkHttp 4 since it's a breaking change.

On Fri, Jun 17, 2016 at 9:20 AM Ken Van Hoeylandt [email protected]
wrote:

MockWebServer currently implements TestRule.
It is probably better to have a separate MockWebServerRule.
This would allow developers to use MockWebServer without the junit
dependency.

Optionally, MockWebServerRule could be moved in to a mockwebserver-junit
package for all junit-related functionality.


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/square/okhttp/issues/2635, or mute the thread
https://github.com/notifications/unsubscribe/AAEEEU8r50RU8V-MqdwYscXtsi2EKAC6ks5qMp8hgaJpZM4I4Wam
.

It’s tempting to grow MockWebServer in scope and use it for things other than testing. That’d probably end up being a new thing, like an OkHttpServer or something. For now we’re in no hurry to do that work.

If you want a webserver for use beyond testing, you might want something else!

The reasoning was that if you use it for mocking during testing, it might be useful for a demo-mode of that same application too.

Either way, it's probably better to have a separate MockWebServerRule anyway, because that keeps the MockWebServer code cleaner.

Just to note that we use TestNG, so having MockWebServer pull in junit is a pain.

This doesn't have to wait until OkHttp 4 though. A new class MockWebServerLight could be added that did not have the dependency, and the pom.xml could be changed to declare the junit dependency as optional. Even if the pom.xml part was considered too risky, at least end users could exclude it in their pom.

I have been a regular offender of making little fake servers using MWS and
a custom dispatcher. These are often used in unit tests, so junit hasn't
been an issue, but also dont feel strongly about it.

Ps MWS has been alive a long time! Here here!

That would make it really easy to test business class that are using Retrofit as client 👍
Just like @JakeWharton said I don't think this will be possible on OkHttp 3.+

Similarly with JUnit 5: MockWebServer pulls in JUnit 4 which is undesirable, so a standalone version would be very useful.

Ugly but functioning workaround for now is to create a fake empty org.junit.rules.ExternalResource class.

I'm closing mockwebserver issues like this. We should probably pull out to a separate library rather than bundling updates with OkHttp.

Was this page helpful?
0 / 5 - 0 ratings