How To Create A New Bundle Object?
I'm trying to use Firebase Analytics for an Android application, and in order to log events I've followed https://firebase.google.com/docs/analytics/android/events. That is, in ord
Solution 1:
As pointed out by Tanis.7x in a comment to my original question, all Android framework classes need to be mocked as the android.jar used for running unit tests is empty as documented here.
Here's an updated version of my original simplified test case:
import android.os.Bundle;
import org.junit.Test;
import org.mockito.Mockito;
import static junit.framework.Assert.assertEquals;
publicclassSimpleTest{
@Testpublic void test() {
Bundle bundleMock = Mockito.mock(Bundle.class);
Mockito.doReturn("click").when(bundleMock).getString("eventType");
Mockito.doReturn(new Long(5542)).when(bundleMock).getLong("eventId");
Mockito.doReturn(new Long(5)).when(bundleMock).getLong("quantity");
Mockito.doReturn("USD").when(bundleMock).getString("currency");
assertEquals("Did not find eventType=click in bundle", "click", bundleMock.getString("eventType"));
}
}
The main difference is, that the variables I set earlier with simple getters are now set by using the appropriate functions of Mockito. The code is not as easy on the eyes, but it should allow me to obtain the wanted behaviour.
Solution 2:
Try using .equals()
to compare strings as assertEquals()
also uses the .equal()
method for its working.
Post a Comment for "How To Create A New Bundle Object?"