Library Resources with Robolectric 3 - JodaTime

Get a ResourceNotFoundException when using the library with Robolectic 3.0-rc3. The resource is declared in build.gradle with the compilation 'net.danlew: android.joda: 2.8.0'. In particular, this is the Android Joda-Time port.

android.content.res.Resources$NotFoundException: Unable to find resource ID #0x7f0501da at org.robolectric.shadows.ShadowResources.checkResName(ShadowResources.java:343) at org.robolectric.shadows.ShadowResources.getResName(ShadowResources.java:333) at org.robolectric.shadows.ShadowResources.openRawResource(ShadowResources.java:382) at android.content.res.Resources.openRawResource(Resources.java) at net.danlew.android.joda.ResourceZoneInfoProvider.openResource(ResourceZoneInfoProvider.java:120) at net.danlew.android.joda.ResourceZoneInfoProvider.<init>(ResourceZoneInfoProvider.java:39) 

Application Class:

 @Override public void onCreate() { super.onCreate(); JodaTime.init(this); } 

My test class:

 @RunWith(RobolectricGradleTestRunner.class) @Config(constants = BuildConfig.class, sdk = 21) public class MyTest { @Before public void setup() { } @Test public void myTest() { //Test my stuff } 

}

+5
source share
1 answer

You need to initialize the library in your tests using the Robolectric runtime. So add this to your setup() methods.

 JodaTimeAndroid.init(RuntimeEnvironment.application); 

So your test will look something like this:

 @RunWith(RobolectricGradleTestRunner.class) @Config(constants = BuildConfig.class, sdk = 21) public class MyApplicationTest { @Before public void setup() { JodaTimeAndroid.init(RuntimeEnvironment.application); } @Test public void myTest() { //Test my stuff DateTime aDateTime = new DateTime(); DateTime bDateTime = new DateTime(aDateTime); assertEquals(aDateTime, bDateTime); } } 
0
source

All Articles