在Robolectic 3.0-rc3中使用库时获取ResourceNotFoundException。资源在build.gradle中使用编译'net.danlew:android.joda:2.8.0'进行声明。具体来说,这是Joda-Time的Android端口。
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)
应用类别:
@Override
public void onCreate() {
super.onCreate();
JodaTime.init(this);
}
我的测试课:
@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class,
sdk = 21)
public class MyTest {
@Before
public void setup() {
}
@Test
public void myTest() {
//Test my stuff
}
}
最佳答案
您需要使用Robolectric运行时环境在测试中初始化库。因此,将其添加到setup()
方法中。
JodaTimeAndroid.init(RuntimeEnvironment.application);
因此您的测试将如下所示:
@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);
}
}