gpt4 book ai didi

java - Robolectric @config 指定 list 路径时,抛出空指针异常

转载 作者:太空宇宙 更新时间:2023-11-03 13:18:54 26 4
gpt4 key购买 nike

我第一次尝试使用 Robolectric。我阅读了互联网上的所有文档,并准备了以下测试代码以供开始使用。在准备代码时,我利用了这些资源 =>

https://github.com/codepath/android_guides/wiki/Robolectric-Installation-for-Unit-Testing

https://github.com/codepath/android_guides/wiki/Android-Unit-and-Integration-testing

http://www.vogella.com/tutorials/Robolectric/article.html

@RunWith( RobolectricTestRunner.class )
@Config( constants = BuildConfig.class, sdk = 21, manifest = "src/main/AndroidManifest.xml" )
public class RemovePeriodTest {

@Before
public void setUp() throws Exception {

}

@Test
public void testCheckRemoveButtonControl() throws Exception {
daySettingsActivity = Robolectric.buildActivity( DaySettingsActivity.class ).create().get();
ListView l = ( ListView ) daySettingsActivity.findViewById( R.id.settings_page );
assertNotNull( "test", l );
}


@After
public void tearDown() throws Exception {


}
}

它抛出以下错误。

java.lang.NullPointerException
at org.robolectric.manifest.MetaData.init(MetaData.java:55)
at org.robolectric.manifest.AndroidManifest.initMetaData(AndroidManifest.java:343)
at org.robolectric.res.builder.DefaultPackageManager.addManifest(DefaultPackageManager.java:417)
at org.robolectric.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:74)
at org.robolectric.RobolectricTestRunner.setUpApplicationState(RobolectricTestRunner.java:433)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:240)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:188)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:152)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

java.lang.RuntimeException: java.lang.NullPointerException
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:244)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:188)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:152)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.lang.NullPointerException
at org.robolectric.manifest.MetaData.init(MetaData.java:55)
at org.robolectric.manifest.AndroidManifest.initMetaData(AndroidManifest.java:343)
at org.robolectric.res.builder.DefaultPackageManager.addManifest(DefaultPackageManager.java:417)
at org.robolectric.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:74)
at org.robolectric.RobolectricTestRunner.setUpApplicationState(RobolectricTestRunner.java:433)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:240)
... 18 more

因为我不能指定 list 文件,如下所示,它会抛出这个错误=>

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

是未指定Manifest路径时出现的代码

@RunWith( RobolectricTestRunner.class )
@Config( constants = BuildConfig.class, sdk = 18, manifest = Config.NONE )
public class RemovePeriodTest {

最佳答案

这里是我们设置我们的测试:

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21)
public class TrackerFragmentViewTest {
}

注意不同的运行者,也没有 list 规范

关于java - Robolectric @config 指定 list 路径时,抛出空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31709528/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com