使用 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);
}
}