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:
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
: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 isnull
).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.As described in Building Local Unit Tests,
Your test code runs against a stripped version of
Bundle
which doesn't contain the actual implementation, hence you getnull
whenever you try to get something from it. If you actually want to test the behavior of aBundle
, I'd suggest writing an instrumentation test which runs on an Android device against the realBundle
implementation.