Hui Wang Hui Wang - 1 month ago 11
Android Question

Android Bundle simple unit test not working

I'm quite new to android and trying to understand how bundle works.

I get blocked by the following unit test. Can someone please explain why it fails ?

@Test
public void testBundle() throws Exception {
Bundle bundle = new Bundle();
String key = "hello";
String value = "world";
bundle.putString(key, value);
Assert.assertEquals(value, bundle.getString(key));
}

junit.framework.ComparisonFailure:
Expected :world
Actual :null

Answer

JUnit tests run on a local machine which doesn't have all the Android source code present, but just stub classes (described here). These stub classes allow you to compile your Android app against them (because their API is identical to the actual Android framework), but they do not contain any logic in order to make them "light".

By default, if you attempt to invoke any of the stub methods you get an exception. Something like this:

public Bundle() {
    throw new RuntimeException("Stub!");
}

this "fail fast" approach was employed in order to prevent developers from accidentally running their code against these stub classes and then wondering why it doesn't work.

However, this behavior can be changed with this configuration in build.gradle:

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

this makes the stub methods return default value instead of throwing exceptions.

You probably have this feature enabled, therefore when you run your JUnit tests you don't get exception, but Bundle#getString() method just returns default value (which is null).

If you want to test code that has Android framework dependencies, you should do either of:

  1. Mock these dependencies (e.g. Mockito)
  2. Run tests with Robolectric
  3. Run instrumentation tests on Android device

In any case, unitTests.returnDefaultValues = true is a VERY DANGEROUS feature to use, because it makes your tests non-reliable: some test can pass because a default value was returned by stub method, but the functionality will fail on a real device. Turn it off.