Currently, running code that involves resources is very hard to write unit tests for.
A common error is ContextUtils not having an application context set, but just calling ContextUtils#initApplicationContext fixes it easily enough. Still a bit irritating to hit it.
What is harder is when values, drawables etc are loaded. Robolectric is supposed to allow loading resources, but I think it has issues with some paths and it fails. Sample trace:
[ RUN ] org.chromium.chrome.browser.suggestions.TileGridTest.testInitialisation
android.content.res.Resources$NotFoundException: unknown resource 2131427652
at org.robolectric.shadows.ShadowAssetManager.getAndResolve(ShadowAssetManager.java:351)
at org.robolectric.shadows.ShadowAssetManager.getResourceValue(ShadowAssetManager.java:102)
at android.content.res.AssetManager.getResourceValue(AssetManager.java)
at android.content.res.Resources.getValue(Resources.java:1229)
at android.content.res.Resources.getDimensionPixelSize(Resources.java:648)
at org.chromium.chrome.browser.suggestions.TileGroup.<init>(TileGroup.java:134)
at org.chromium.chrome.browser.suggestions.TileGrid.<init>(TileGrid.java:27)
at org.chromium.chrome.browser.suggestions.TileGridTest.testInitialisation(TileGridTest.java:71)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:52)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:48)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:238)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:175)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:53)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:139)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.chromium.testing.local.GtestComputer$GtestSuiteRunner.run(GtestComputer.java:46)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at org.chromium.testing.local.JunitTestMain.main(JunitTestMain.java:105)
It would be nice if LocalRobolectricTestRunner or some base test class handled all that.
Comment 1 by jbudorick@chromium.org
, Feb 17 2017