使用Robolectric 3的图书馆资源 - JodaTime

时间:2015-06-18 18:35:22

标签: android jodatime robolectric android-testing

使用带有Robolectic 3.0-rc3的库时获取ResourceNotFoundException。资源在build.gradle中使用compile'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
}

}

1 个答案:

答案 0 :(得分:-1)

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