vendredi 9 septembre 2016

How to mock internal methods of a static method in Android/Java?

I have a method to convert my bitmaps to Base64 as follows. I need to write a Junit test for the same. However i have some Android dependencies like Bitmap, Base64 etc.

public static String convertBitmapToBase64(Bitmap imageBitmap) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    imageBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] byteArray = stream .toByteArray();
    return Base64.encodeToString(byteArray, Base64.DEFAULT);
}

Below is the code i wrote in my test unit class.

@Test
public void testBitmaps() {
    Bitmap bitmap = Mockito.mock(Bitmap.class);
   // Mockito.when(Base64.encodeToString(null,0)).thenReturn("");
    String result = ImageLoaderUtils.convertBitmapToBase64(bitmap);
    assert (!result.isEmpty());
}

However i am getting the following error while compiling the unit test.

Method encodeToString in android.util.Base64 not mocked. 

How can i mock the methods called from the static method ?

Aucun commentaire:

Enregistrer un commentaire