Skip to content
Advertisement

Android JUnit3 test ParcelUuid getUuid() returning null for non-null UUID?

I have the following code in an Android JUnit3 unit test:

public void testWTF()
{
    UUID uuidExpected = UUID.fromString("00001234-0000-1000-8000-00805f9b34fb");
    ParcelUuid parcelUuid = new ParcelUuid(uuidExpected);
    UUID uuidActual = parcelUuid.getUuid();
    Assert.assertEquals("uuidExpected != uuidActual", uuidExpected, uuidActual);
}

getUuid() is returning null, but the UUID passed to the ParcelUuid is non-null and valid.

What gives?

Is there something wrong w/ using the Android runtime library in a JUnit test?

Thanks!

Pv

Advertisement

Answer

Revisiting over 5.5 years later after I saw someone upvoted.

Answering my own question.

Per my experience and https://developer.android.com/training/testing/unit-testing/local-unit-tests#error-not-mocked

Any Android API call in a unit test by default throws an exception.
This is by design to force you to mock anything that you call.
You can change this default behavior with the following in your module’s build.gradle:

android {
    ...
    testOptions {
        unitTests.returnDefaultValues = true
    }
}

Doing so will result in any Android API call being a no-op that always returns null or 0.
This must have been what I was doing >5.5 years ago.
This is OK while developing test, but is not advised to leave in permanently.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement