我正在使用Roboletric进行一些测试,但我遇到了一个我无法解决的问题。运行测试时,出现以下错误:
android.content.res.Resources $ NotFoundException:无法在包[android,org.robolectric.default]中找到资源ID#0x7f09001c
at org.robolectric.shadows.ShadowAssetManager.getResName(ShadowAssetManager.java:925)
at org.robolectric.shadows.ShadowAssetManager.loadXmlResourceParser(ShadowAssetManager.java:439)
at org.robolectric.shadows.ShadowResources.loadXmlResourceParser(ShadowResources.java:221)
在android.content.res.Resources.loadXmlResourceParser(Resources.java)
在android.content.res.Resources.getLayout(Resources.java:852)
在android.view.LayoutInflater.inflate(LayoutInflater.java:394)
在android.view.LayoutInflater.inflate(LayoutInflater.java:352)
在com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
在android.app.Activity.setContentView(Activity.java:1867)
at com.example.robertoassad.roboletrictest.MainActivity.onCreate(MainActivity.java:15)
在android.app.Activity.performCreate(Activity.java:5008)
at org.robolectric.util.ReflectionHelpers.callInstanceMethod(ReflectionHelpers.java:232)
at org.robolectric.android.controller.ActivityController $ 1.run(ActivityController.java:58)
at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:364)
at org.robolectric.shadows.CoreShadowsAdapter $ 1.runPaused(CoreShadowsAdapter.java:26)
at org.robolectric.android.controller.ActivityController.create(ActivityController.java:55)
at org.robolectric.android.controller.ActivityController.create(ActivityController.java:65)
at org.robolectric.android.controller.ActivityController.setup(ActivityController.java:157)
at org.robolectric.Robolectric.setupActivity(Robolectric.java:101)
at com.example.robertoassad.roboletrictest.MainActivityTest.clickingLogin_shouldStartLoginActivity(MainActivityTest.java:20)
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.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.robolectric.RobolectricTestRunner $ HelperTestRunner $ 1.evaluate(RobolectricTestRunner.java:523)
at org.robolectric.internal.SandboxTestRunner $ 2.evaluate(SandboxTestRunner.java:226)
at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:108)
at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:35)
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.internal.SandboxTestRunner $ 1.evaluate(SandboxTestRunner.java:62)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner $ Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)
在
com.example.robertoassad.alltestsmerge.MainActivity.onCreate(MainActivity.java:15)
在MainActivity.java:15上,错误在以下代码中:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
具体在:setContentView(R.layout.activity_main);
和
com.example.robertoassad.roboletrictest.MainActivityTest.clickingLogin_shouldStartLoginActivity(MainActivityTest.java:20)
在MainActivityTest.java:20上,错误具体在:
MainActivity activity = Robolectric.setupActivity(MainActivity.class);
对我来说,我在这个问题上看不出来......
详情:
测试类位于文件夹:app\src\test\java\{package}
测试是:
@RunWith(RobolectricTestRunner.class)
@Config(manifest= Config.NONE)
public class MainActivityTest {
@Test
public void clickingLogin_shouldStartLoginActivity() {
MainActivity activity = Robolectric.setupActivity(MainActivity.class);
activity.findViewById(R.id.login).performClick();
Intent expectedIntent = new Intent(activity, HomeActivity.class);
Intent actual = ShadowApplication.getInstance().getNextStartedActivity();
assertEquals(expectedIntent.getComponent(), actual.getComponent());
}
}
答案 0 :(得分:4)
这对我有用:
去Robolectric Setup page并按照他们的指南转到他们的最新版本。
换句话说 ,
testImplementation "org.robolectric:robolectric:3.7.1"
includeAndroidResources = true
build.graddle
例如,
android {
testOptions {
unitTests {
includeAndroidResources = true
}
}
}
dependencies {
testImplementation "org.robolectric:robolectric:3.7.1"
}
在没有任何配置注释的情况下运行我的测试
@RunWith(RobolectricTestRunner.class)
public class MyTests {
@Test
public void singleTest() {
//Do and verify or assert something
}
}
答案 1 :(得分:0)
您需要在@Config中指定清单或常量,以便Robolectric知道在哪里找到资源。 Config.NONE无法正常工作。