Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Robolectric + Maven

I am new to using Robolectric with Maven for an Android project.

Here is the test case (it doesn't really do anything)

@RunWith(RobolectricTestRunner.class)
public class UTest {

private SplashActivity mActivity;

@Before
public void setUp() throws Exception {
    mActivity = Robolectric.buildActivity(SplashActivity.class).create().get();
    ProgressBar bar = (ProgressBar) mActivity.findViewById(R.id.signInProgress);
}



@Test
public void testingMe() throws Exception {

}
}

However, when this is run against maven using the target test, I get the following exception:

   java.lang.IllegalStateException: immutable!
    at org.robolectric.res.ResBundle$ResMap.merge(ResBundle.java:128)
    at org.robolectric.res.ResBundle$ResMap.access$100(ResBundle.java:116)
    at org.robolectric.res.ResBundle.mergeLibraryStyle(ResBundle.java:88)
    at org.robolectric.res.OverlayResourceLoader.doInitialize(OverlayResourceLoader.java:26)
    at org.robolectric.res.XResourceLoader.initialize(XResourceLoader.java:29)
    at org.robolectric.res.XResourceLoader.getValue(XResourceLoader.java:53)
    at org.robolectric.res.OverlayResourceLoader.getValue(OverlayResourceLoader.java:58)
    at org.robolectric.res.RoutingResourceLoader.getValue(RoutingResourceLoader.java:31)
    at org.robolectric.shadows.ShadowAssetManager.getAndResolve(ShadowAssetManager.java:263)
    at org.robolectric.shadows.ShadowAssetManager.getAndResolve(ShadowAssetManager.java:259)
    at org.robolectric.shadows.ShadowAssetManager.getResourceText(ShadowAssetManager.java:62)
    at android.content.res.AssetManager.getResourceText(AssetManager.java)
    at android.content.res.Resources.getText(Resources.java:225)
    at android.content.res.Resources.getString(Resources.java:313)
    at ****.loadRestServerUrl(****.java:417)
    at ****.onCreate(****.java:201)
    at org.robolectric.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:146)
    at org.robolectric.RobolectricTestRunner.setUpApplicationState(RobolectricTestRunner.java:387)
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:227)
    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.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:264)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:153)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:124)
    at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:200)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:153)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103)

Does anyone know what the problem could be? I am using Robolectric 2.1.1.

like image 323
T Lam Avatar asked Nov 23 '22 17:11

T Lam


1 Answers

This was an issue with multiple threads accessing resources, causing Robolectric to attempt to load the resources concurrently.

I fixed this in https://github.com/robolectric/robolectric/pull/2353. If you use the latest snapshot, it should work, or you can wait for the 3.1 release.

As a temporary measure, if you just force loading a resource in Application#onCreate() (such as by calling getString(R.string.whatever) or some other method that will be run before any additional threads are created, it will ensure the resources have been loaded before multiple threads try to use them.

like image 175
Scott Kennedy Avatar answered Nov 26 '22 05:11

Scott Kennedy