I'm trying to check if my API is available within a unit test, to be sure that it responds with 200.
Now, my problem is that I'm not really sure when to use Local Test and when I have to use Android Instrumentation Tests. I know that I have to use Instrumented Tests for UI testing but how to test the endpoint?
I use Retrofit2 for communication. And tried to test Endpoint in 2 ways with Local Tests.
Example 1 (synchronous, does not work)
public class EndpointTest {
EndpointApi api;
SimpleInjection simpleInjection;
@Before
public void setUp() {
simpleInjection = new SimpleInjection();
api = simpleInjection.getEndpointApi();
}
@Test
public void endpoint_1_isAvailable() {
Call<ApiResponse> rootCall = api.getRoot();
try {
int reponseCode = rootCall.execute().code();
Assert.assertEquals(200, reponseCode);
} catch (IOException e) {
Assert.fail();
}
}
}
Example 2 (asynchronous, does work)
public class EndpointTest {
EndpointApi api;
SimpleInjection simpleInjection;
@Before
public void setUp() {
simpleInjection = new SimpleInjection();
api = simpleInjection.getEndpointApi();
}
@Test
public void endpoint_2_isAvailable() {
Call<ApiResponse> rootCall = api.getRoot();
rootCall.enqueue(new Callback<ApiResponse>() {
@Override
public void onResponse(Call<ApiResponse> call, Response<ApiResponse> response) {
Assert.assertEquals(200, response.code());
}
@Override
public void onFailure(Call<ApiResponse> call, Throwable t) {
Assert.fail();
}
});
}
}
Do I have to use Android Instrumentation Test for asynchronous mode?