2015-06-04 17 views
8

Android Studio'da birim testi için Robolectric 3.0 RC3 kullanıyorum. Ve testi her çalıştırdığımda bir ResourceNotFoundException alıyorum. Lütfen sorunu çözmeme yardımcı olun.

build.gradle

dependencies { 
    testCompile 'junit:junit:4.12' 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    compile 'com.android.support:appcompat-v7:22.0.0' 
    testCompile 'org.mockito:mockito-core:1.9.5' 
    testCompile 'org.robolectric:robolectric:3.0-rc3' 
    compile 'com.ibm.icu:icu4j:53.1' 
} 

Testi Sınıf

@Before 
public void setUp() throws Exception { 
    activity = Robolectric.buildActivity(MainActivity.class).create().get(); 
} 
@Test 
public void pressingTheButtonShouldStartTheListActivity() throws Exception { 
    //Something to test 
} 

Yani, her ne zaman Robolectric.buildActivity() ben Resources$NotFoundException olsun. Kaynak #0x7f040016, R.layout.activity_main'a (xml) işaret eder.

android.content.res.Resources$NotFoundException: Unable to find resource ID #0x7f040016 
at org.robolectric.shadows.ShadowResources.checkResName(ShadowResources.java:343) 
at org.robolectric.shadows.ShadowResources.resolveResName(ShadowResources.java:338) 
at org.robolectric.shadows.ShadowResources.loadXmlResourceParser(ShadowResources.java:429) 
at android.content.res.Resources.loadXmlResourceParser(Resources.java) 
at android.content.res.Resources.getLayout(Resources.java:852) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:394) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256) 
at android.app.Activity.setContentView(Activity.java:1867) 
at com.aricent.anas.unittesting2.MainActivity.onCreate(MainActivity.java:15) //****HERE**** 
at android.app.Activity.performCreate(Activity.java:5008) 
at org.robolectric.util.ReflectionHelpers.callInstanceMethod(ReflectionHelpers.java:195) 
at org.robolectric.util.ActivityController$1.run(ActivityController.java:122) 
at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:305) 
at org.robolectric.shadows.CoreShadowsAdapter$2.runPaused(CoreShadowsAdapter.java:45) 
at org.robolectric.util.ActivityController.create(ActivityController.java:118) 
at org.robolectric.util.ActivityController.create(ActivityController.java:129) 
at com.aricent.anas.unittesting2.MainActivityTest.setUp(MainActivityTest.java:32) //****HERE**** 
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) 
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) 
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24) 
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:245) 
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:185) 
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54) 
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:149) 
at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 
at org.junit.runner.JUnitCore.run(JUnitCore.java:137) 
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140) 


Process finished with exit code -1 

MainActivity.java:15 MainActivityTest.java:32setContentView(R.layout.activity_main);

olduğunu activity = Robolectric.buildActivity(MainActivity.class).create().get();

+0

Test dizini yapılandırmada çalışma dizini nedir? –

+0

@EugenMartynov $ MODULE_DIR $ –

+0

Etkinlikte bazı özel görünümleriniz var mı? –

cevap

3

test sınıfı doğru açıklamalı mı olduğunu trace- yığın kimliği takip etmek mi? Ve bir manifest dosyasına doğru yolu kullanıyor mu? Aşağıdaki benim için işler: Yapılandırma ek açıklama Çıkarma

@RunWith(RobolectricTestRunner.class) 
@Config(manifest = "src/main/AndroidManifest.xml", emulateSdk = Build.VERSION_CODES.LOLLIPOP) 
public class MyActivityTest { .... } 

Bahsettiğiniz ResourcesNotFound duruma neden olur.

+3

'manifest =" src/main/AndroidManifest.xml "kısmı bu konuda yardımcı olabilir, ancak size daha fazla ve daha fazla sorun verecektir. "RobolectricTestRunner" yerine "RobolectricGradleTestRunner" – nenick

+0

kullanın. Ayrıca "Mac kullanıcıları için Not" http://robolectric.org/getting-started/ – nenick

+0

'emulateSdk 'ifadesini kullanın ve Robolectric 3.0'dan kaldırılır,' sdk' kullanın. –

0

Bu, Robolectric! Open Gradle, ProjectName>: app> Görevler> doğrulama> testDebugUnitTest üzerine çift tıklayın. Like in picture Daha fazla bilgi here

İlgili konular