Android unit test / mockito: android.location.location is not simulated
I'm trying to learn basic JUnit and mockito tests on Android. I'm trying to write unit tests for a simple class that handles activities that represent location information to find users' locations from location services
I've been trying to create fake locations for testing:
@Test
public void testLocationReceived() throws Exception {
Location fakeLocation = new Location(LocationManager.NETWORK_PROVIDER);
fakeLocation.setLongitude(100);
fakeLocation.setLatitude(-80);
...
}
But I got a mistake:
java.lang.RuntimeException: Method setLongitude in android.location.Location not mocked.
I know that unit tests on Android run on the JVM, so you don't have access to anything that requires an operating system / framework, but is this one of them?
>If so, how do you know which classes can / cannot be used on the JVM? > Do I still need instrument / equipment testing now? JVM based unit testing is just for testing this class?
resolvent:
You should add the following to build.gradle (APP):
testOptions {
unitTests.returnDefaultValues = true
}
Details: http://tools.android.com/tech-docs/unit-testing-support#TOC -Method-…-not-mocked.-