Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?
like image 633
user3200382 Avatar asked Apr 25 '16 04:04

user3200382


2 Answers

You should add the following in build.gradle (app):

testOptions {
        unitTests.returnDefaultValues = true
}

More detail: http://tools.android.com/tech-docs/unit-testing-support#TOC-Method-...-not-mocked.-

like image 183
John Huang Avatar answered Nov 05 '22 04:11

John Huang


I had the same pb as you. @John Huang answer helped me. You first need to mock the location then use mockito when to put the values that you wanted .here is my code

@RunWith(PowerMockRunner::class)
class MapExtensionTest {
 @Mock
    private lateinit var location: Location

    //region calculateDistance
    @Test
    fun `given a valid store and a valid location  to calculateDistance should return the correct distance`() {
        Mockito.`when`(store.coordinate).thenReturn(coordinate)
        Mockito.`when`(coordinate.latitude).thenReturn(FAKE_LAT)
        Mockito.`when`(coordinate.longitude).thenReturn(FAKE_LON)
        Mockito.`when`(location.latitude).thenReturn(FAKE_LAT1)
        Mockito.`when`(location.longitude).thenReturn(FAKE_LON1)
        val result = FloatArray(1)
        Location.distanceBetween(
            store.coordinate.latitude,
            store.coordinate.longitude,
            location.latitude, location.longitude, result
        )

        store.calculateDistance(location)

        Assert.assertTrue(store.distance == result[0].toDouble())
    }

Dont forget this like john said

testOptions {
    unitTests.returnDefaultValues = true
}

If you have pb with import here is my test dependency but i dont remember witch one is important for this example so keep in mind that you may not need all

//testing dependencies
testImplementation "junit:junit:$junitVersion"
testImplementation "org.mockito:mockito-inline:${mockitoInlineVersion}"
testImplementation "androidx.arch.core:core-testing:${coreTestingVersion}"
testImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:${mockitoKotlinVersion}"
androidTestImplementation "org.mockito:mockito-android:${mockitoAndroidVersion}"
testImplementation group: 'org.powermock', name: 'powermock-api-mockito2', version: "${powerMockMockitoVersion}"
testImplementation group: 'org.powermock', name: 'powermock-module-junit4', version: "${powerMockjUnitVersion}"
like image 35
Dagnogo Jean-François Avatar answered Nov 05 '22 03:11

Dagnogo Jean-François