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.-

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>