jeudi 3 mars 2016

Android Local Tests vs Instrumented Test

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 (asynchronous, 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 (synchronous, 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?

Aucun commentaire:

Enregistrer un commentaire