【问题标题】:Robolectric - BuildActivity(MyActivity.class).create() throw NullPointerExceptionRobolectric - BuildActivity(MyActivity.class).create() 抛出 NullPointerException
【发布时间】:2016-01-28 11:34:31
【问题描述】:

我在 Eclipse 中创建的 Android Studio 中有项目。我将它迁移到 Gradle 并添加了测试。对于测试,我使用 Robolectric。当我在项目中测试库模块时,一切正常,但是当我开始测试应用程序模块并调用 BuilActivity(MyActivity.class).create().get() 时,控制台会打印出该错误:

java.lang.NullPointerException
    at org.robolectric.res.builder.DefaultPackageManager.getActivityInfo(DefaultPackageManager.java:173)
    at org.robolectric.util.ActivityController.getActivityInfo(ActivityController.java:65)
    at org.robolectric.util.ActivityController.attach(ActivityController.java:51)
    at org.robolectric.util.ActivityController$1.run(ActivityController.java:121)
    at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:304)
    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 test(test.java:33)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    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:251)
    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: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:152)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runners.Suite.runChild(Suite.java:128)
    at org.junit.runners.Suite.runChild(Suite.java:27)
    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.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    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)

我不知道是什么导致了这个错误。有人有这样的吗?

编辑
在项目中我使用 7 个模块。当我在没有 activite 的情况下开始测试一个模块时,一切正常,但是当我开始使用 Activity 测试模块时,我得到了上面的错误堆栈跟踪。
Activity
在活动中,附加了带有 viewpager 的片段,活动开始使用 volley 下载数据。此活动扩展了其他类,该类扩展了另一个类,等等等等……
buil.gradle

apply plugin: 'com.android.application'

dependencies {
    compile project(':proj-core')
    compile project(':proj-gui-base')
    testCompile "org.robolectric:robolectric:3.0"
    testCompile "org.robolectric:shadows-support-v4:3.0"
    testCompile 'junit:junit:4.12'
}

android {

    compileSdkVersion 19
    buildToolsVersion "23.0.0"

    repositories {
        maven { url 'http://repo1.maven.org/maven2' }
    }
    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src', 'src/main/java']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }

        test {
            resources.srcDirs = ['test']
            java.srcDirs = ['test', 'test/src/java']
        }

        robolectric.java.srcDir file('test/src/java')

        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

测试
问题始于@Before 方法

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class)
public class Test {

    Context context;
    ShadowApplication shadowApplication;
    HomeActivity homeActivity;

    @Before
    public void setUp() {
        RuntimeEnvironment.application.onCreate();
        context = RuntimeEnvironment.application.getApplicationContext();
        shadowApplication = Shadows.shadowOf(RuntimeEnvironment.application);
        homeActivity = Robolectric.setupActivity(HomeActivity.class);
    }

    @Test
    public void test_core_base_popup_popup() {

        Popup popup = new Popup(Popup.Type.GENERIC);
        PopupQueueManager.addPopup(homeActivity, popup);
        PopupQueueManager.displayAllPopups(homeActivity);
        assertEquals(popup.getContent(), "xyz");
    }
}

项目结构
我只描述了 7 个模块中的一个。每个模块都有相同的结构。

proj-android  
|  
|-proj-core-base  
    |-assets  
    |-res
    |-src
       |- com.package
    |-test
       |-java
          |-Test.java
|-next-module  
|-next-next-module  
|-etc

【问题讨论】:

  • 你能分享一下build.gradle,test和activity onCreate代码吗?
  • 我已经编辑了我的帖子。 @EugenMartynov
  • 您不需要调用应用程序oncreate,它将由Robolectric 完成。还要删除上下文和影子应用程序,因为您不使用它。并将sdk = 21 添加到您的配置注释中以进行测试
  • 所以,我删除了您推荐的内容并将 sdk 添加到注释中,并且 ... .... 不再起作用。我得到同样的错误。

标签: android eclipse unit-testing android-studio robolectric


【解决方案1】:

我使用的是 Android Studio 3.1.1,并且在调用 Robolectric.buildActivity(...).create().get() 时也遇到了 NullPointerException。然后我找到了以下链接:

http://robolectric.org/getting-started/

建议将以下代码块添加到应用的 build.gradle 文件中:

android {
    testOptions {
        unitTests { 
            includeAndroidResources = true 
        } 
    } 
}

通过此添加,避免了异常。我不确定此解决方案是否适用于您的情况。只是分享。

【讨论】:

    【解决方案2】:

    我猜你使用的是不兼容的 robolectric 和 android SDK 版本:

    使用 Robolectric 3,您应该使用:

    MyActivity activity = Robolectric.setupActivity(MyActivity.class);
    

    【讨论】:

    • 我试过这个,但没有任何积极作用
    猜你喜欢
    • 2012-11-23
    • 2017-08-11
    • 2013-09-07
    • 2013-06-18
    • 2016-07-07
    • 2014-10-02
    • 2012-05-21
    • 2014-08-15
    • 2013-08-06
    相关资源
    最近更新 更多