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
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With