I try to use MockWebServer to the various responses to my API. I have made a simple example just to try that what I would like to do is a working method.
Isn't the mockWebServer meant to 'mock' the endpoint of my http connections? Like a real server? Whenever I try to make a call I got UnknownHostException.
Am I using it wrong? Isn't it supposed to just replace a server's response? (mock it)
E D I T:
I have internet permission in manifest.
I use:
androidTestImplementation 'com.squareup.okhttp3:mockwebserver:3.9.1'
Code:
@Test
public void myTest() throws IOException, InterruptedException {
String url = "http://some-mock-url.com";
MockWebServer server = new MockWebServer();
server.enqueue(new MockResponse().setBody("Something not valid JSON response"));
server.start();
server.url(url);
final CountDownLatch signal = new CountDownLatch(1);
AndroidNetworking.get(url)
.addQueryParameter("some_key", "some_value12345")
.addHeaders("token", "token_1231234")
.build()
.getAsJSONObject(new JSONObjectRequestListener() {
@Override
public void onResponse(JSONObject response) {
signal.countDown();
Log.i("Response", "jsonObject: " + response);
}
@Override
public void onError(ANError anError) {
signal.countDown();
Log.i("Response", "error: " + anError.getMessage());
}
}
);
signal.await();
}
Logcat:
com.androidnetworking.error.ANError: java.net.UnknownHostException: Unable to resolve host "some-mock-url.com": No address associated with hostname