dimanche 24 avril 2016

Android Unit Testing / Mockito: android.location.Location not mocked

I am trying to learn basic JUnit and Mockito testing on Android. I'm trying to write unit tests for a simple class that handles finding the user's location from Location Services on behalf of activities that need location information.

I have been trying to create "faked location" to test with:

@Test
public void testLocationReceived() throws Exception {
    Location fakeLocation = new Location(LocationManager.NETWORK_PROVIDER);
    fakeLocation.setLongitude(100);
    fakeLocation.setLatitude(-80);
    ...  
}

But I get the error:

java.lang.RuntimeException: Method setLongitude in android.location.Location not mocked.

I understand that unit tests on Android run on the JVM, so you don't have access to anything that requires the operating system / framework, but is this one of those cases as well?

  • If so, how do you tell what classes can / can't be used on the JVM?
  • Do I now need instrumentation/device tests in addition to JVM-based unit tests just to test this one class?

Aucun commentaire:

Enregistrer un commentaire