okhttp: MockWebserver 2.0 play() does not seem to work.
I have code that uses MockWebServer
, however it seems like the server does not accept incoming connections. This seems to have started happening since we moved to 2.0.0, on 1.6.0 things were working fine.
The dependencies are
compile 'com.squareup.okhttp:okhttp:2.0.0'
compile 'com.squareup.okhttp:okhttp-urlconnection:2.0.0'
androidTestCompile ('com.squareup.okhttp:mockwebserver:2.0.0'){ exclude group: 'com.squareup.okhttp' }
Tried a sample test as below, once play is called, I try to hit the server URL:port using curl/browser, but it does not seem to accept any connection, Just says connection refused. Isn’t it supposed to return the enqueued responses ?
public void testMockWebServer() throws Exception {
// Create a MockWebServer. These are lean enough that you can create a new
// instance for every unit test.
MockWebServer server = new MockWebServer();
// Schedule some responses.
server.enqueue(new MockResponse().setBody("hello, world!"));
server.enqueue(new MockResponse().setBody("sup, bra?"));
server.enqueue(new MockResponse().setBody("yo dog"));
// Start the server.
server.play();
server.getPort();
Thread.sleep(5000000000L);
// Shut down the server. Instances cannot be reused.
server.shutdown();
}
About this issue
- Original URL
- State: closed
- Created 10 years ago
- Comments: 18 (2 by maintainers)
To add to what @josketres mentioned, MockWebServer works fine when run from a Robolectric test. The ECONNREFUSED error is seen only when running instrumentation tests on an emulator/device.