Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Robolectric: NotFoundException: unknown resource 0 at org.robolectric.shadows.ShadowAssetManager.getAndResolve (ShadowAssetManager.java:258)

I met the error when test getView of an apdater. The tested code was calling View.setImageResource(0) during the test case to set an empty image when the exception happen.

I ran test robolectric-2.2-20130712.161723-17-jar-with-dependencies.jar with Eclipse. I tried the replace the ResouceLoader with ShadowResources.createFor(new ResourceLoader() {...}. But it doesnot work. My replaced ResouceLoader was not called.

Anybody know how to solve it?

android.content.res.Resources$NotFoundException: unknown resource 0
at org.robolectric.shadows.ShadowAssetManager.getAndResolve(ShadowAssetManager.java:258)
at org.robolectric.shadows.ShadowAssetManager.getResourceValue(ShadowAssetManager.java:85)
at android.content.res.AssetManager.getResourceValue(AssetManager.java)
at android.content.res.Resources.getValue(Resources.java:1009)
at android.content.res.Resources.getDrawable(Resources.java:658)
at org.robolectric.shadows.ShadowView.buildDrawable(ShadowView.java:92)
at org.robolectric.shadows.ShadowImageView.setImageResource(ShadowImageView.java:38)
at android.widget.ImageView.setImageResource(ImageView.java)
at com.mobile.client.share.edentity.PhotostreamAdapter.getView(PhotostreamAdapter.java:54)
at com.mobile.client.share.edentity.PhotostreamAdapterTest.testGetView(PhotostreamAdapterTest.java:163)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
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.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:241)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:177)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Another similar problem in my roboletric unit test on my Apdater ContentDetailsAdapter is from statement mContext.getResources().getString(R.string.delete_hint_title). The mContext is created by Robolectric.getShadowApplication().getApplicationContext(). Is there something wrong with my config about Roboletric? I was able to call ProgressDialog.show(mContext, "", "") in my unit test. It seems the problem is always with resource. Here is the stack:

android.content.res.Resources$NotFoundException: String resource ID #0x7f07004f
at android.content.res.Resources.getText(Resources.java:229)
at android.content.res.Resources.getString(Resources.java:313)
at com.mobile.client.share.edentity.ContentDetailsAdapter.checkDeletedSelection(ContentDetailsAdapter.java:220)
at com.mobile.client.share.edentity.ContentDetailsAdapter.getCheckedItems(ContentDetailsAdapter.java:178)
at com.mobile.client.share.edentity.SetsAdapterTest.testGetCheckedItems(SetsAdapterTest.java:361)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
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.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:241)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:177)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
like image 752
George_BJ Avatar asked Dec 15 '22 05:12

George_BJ


1 Answers

I had a similar error to your 2nd issue. My environment was set up according to http://www.youtube.com/watch?v=3mjcg5gfDLE

The sample provided works on my Eclipse, but when I tried to link MyProjectTest to my own app, the second error you described appeared.

The video describes the solution at 6:00.

MyProjectTest -> Run As Configuration -> JUnit -> MyProjectTestConfiguration -> Arguments -> Click 'Other' radio button -> Workspace -> select

The take home is: if you run your robolectric tests for a different app, you must point the 'Working Direcotry' to it. Additionally, you need to link your test folder from MyProjectTest to a new test folder in .

like image 186
sudocoder Avatar answered May 12 '23 06:05

sudocoder