vendredi 4 mai 2018

Testing Okhttp request in Android

I'm trying to make a test for a GET request using an OKHTTP 3 client. Here is the code of the method:

protected void methodUnderTest() {

  client.get(url, new HttpHandler() {
      @Override
      public void onSuccess(int result) {
          //Need to see the result here
      }

      @Override
      public void onError(IOException e) {
      }
  });
}

This client is not running in the main thread and is an async task so, when I try to use my test, it's finishing before I get the response on the "onSuccess". Here is my test:

@Test
public void downloadPublicKeyFileCheckIfExists() {

    ClassUnderTest classUnderTest = new ClassUnderTest();
    try {
        classUnderTest.methodUnderTest("examplePublicKey");
        assertThat(conditionToPassTest, is(true));
    } catch (LicenseException e) {
        e.printStackTrace();
        fail();
    }

}

I'm using mockito and powerMock in the tests. Is there any way to test this method?

Aucun commentaire:

Enregistrer un commentaire