Robolectric - BuildActivity(MyActivity.class).create()抛出NullPointerException

时间:2015-10-28 15:39:36

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

我在Android Studio中创建的项目是在Eclipse中创建的。我将它迁移到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个模块。当我开始测试一个没有激活的模块时,一切正常,但当我开始测试模块的活动时,我已经得到了错误堆栈跟踪。
活动
在活动中,附加了带有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

2 个答案:

答案 0 :(得分:1)

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

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

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

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

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

答案 1 :(得分:0)

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

使用Robolectric 3,你应该使用:

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