Robolectric与ActiveAndroid $ ReflectionUtils ActiveAndroid设置NullPointerException异常 [英] Robolectric with ActiveAndroid setup nullpointerexception on ActiveAndroid$ReflectionUtils

查看:608
本文介绍了Robolectric与ActiveAndroid $ ReflectionUtils ActiveAndroid设置NullPointerException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现在现有应用robolectric测试。我可以运行简单的静态测试,但是当我加入测试与ActiveAndroid我得到NullPointerException异常,我认为当单元测试是正在运行ActiveAndroid无法找到。

林不知道,也许有一些配置为ActiveAndroid为Robelectric单元测试。请帮我在这一个。

这是项目结构:

MainApp
结果-src /主
结果-src /测试

为LibraryApp
结果-src /主/型号:LT; ---这里是我的模型和类Android活跃
结果-libs / activeandroid-3.0.jar < ----积极的android jar文件

我进口ActiveAndroid-3.0.jar在图书馆的应用程序。

下面是的build.gradle文件:

MainApp的build.gradle

 安卓{
compileSdkVersion 21
buildToolsVersion22.0.0defaultConfig {
    的applicationIDCOM .....
    18的minSdkVersion
    targetSdkVersion 21
    版本code 1
    的versionName1.0    //启用multidex支持。
    multiDexEnabled真    testInstrumentationRunnerandroid.support.test.runner.AndroidJUnitRunner
}
buildTypes {
    发布 {
        minifyEnabled假
        proguardFiles getDefaultProguardFile('proguard的-android.txt'),'proguard-rules.pro
    }
}dexOptions {
    preDexLibraries = FALSE
}sourceSets {
    主要{
        java的{
            排除COM /谷歌/ GSON /注解/ Expose.class
        }
    }
}lintOptions {
    abortOnError假
}
packagingOptions {
    排除META-INF / NOTICE.txt
    排除META-INF / LICENSE.TXT
}
}
依赖{
编译文件树(导演:'库',包括:['的* .jar'])
编译文件(库/ gcm.jar')
编译com.android.support:multidex:1.0.1
编译乔达时间:乔达时间:2.9.1
androidTestCompile'的JUnit:JUnit的:4.12
testCompile'的JUnit:JUnit的:4.12
编译com.facebook.fresco:壁画:0.7.0+
编译com.github.bluejamesbond:textjustify-安卓2.1.0
编译文件(库/ activeandroid-3.0.jar')
编译com.google.android.gms:播放服务:7.5.0
编译com.jakewharton:奶油刀:7.0.1
编译文件(库/ FlurryAnalytics-5.5.0.jar')
编制项目(':应用程序)
androidTestCompile'c​​om.android.support.test:亚军:0.3
testCompile'c​​om.android.support.test:亚军:0.3androidTestCompile'c​​om.android.support.test:规则:0.3
testCompile'c​​om.android.support.test:规则:0.3
//设置这种依赖关系构建和运行长者preSSO测试
androidTestCompile'c​​om.android.support.test.es preSSO:ES preSSO核心:2.2
//设置这种依赖关系构建和运行UI测试的Automator
androidTestCompile'c​​om.android.support.test.uiautomator:uiautomator-V18:2.1.1
//编译com.squareup.retrofit:改造:2.0.0-β1//类路径com.stanfy.spoon:勺子gradle这个-插件:1.0.2
编译com.github.jakob-GRABNER:圆-进展 - 视图:V1.2.2
编译('com.crashlytics.sdk.android:crashlytics:2.5.5@aar'){
    传递= TRUE;
}
testCompileorg.robolectric:robolectric:3.0 RC3
testCompile'的JUnit:JUnit的:4.12
testCompileorg.robolectric:阴影-multidex:3.0
}

为LibraryApp的build.gradle

 应用插件:'com.android.library安卓{
compileSdkVersion 21
buildToolsVersion22.0.0defaultConfig {
    18的minSdkVersion
    targetSdkVersion 22
    版本code 1
    的versionName1.0    //启用multidex支持。
    multiDexEnabled假
}
buildTypes {
    发布 {
        minifyEnabled假
        proguardFiles getDefaultProguardFile('proguard的-android.txt'),'proguard-rules.pro
    }
}
dexOptions {
    preDexLibraries = FALSE
}packagingOptions {
    排除META-INF / NOTICE.txt
    排除META-INF / LICENSE.TXT
    排除META-INF / LICENSE
    排除META-INF /注意事项
}配置(android.lintOptions){
    abortOnError假
}
}依赖{
编译文件树(导演:'库',包括:['的* .jar'])编译com.balysv.materialmenu:材料菜单工具栏:1.5.4
编译org.lucasr.twowayview:twowayview:0.1.4
编译文件(库/ GSON-2.3.1.jar')
编译org.apache.httpcomponents:httpmime:4.2.3
编译文件(库/ activeandroid-3.0.jar')
编译com.google code.gson:GSON:2.3.1
编译com.squareup.picasso:毕加索:2.5.2
编译com.scottyab:aescrypt:0.0.1
编译com.android.support:multidex:1.0.1testCompileorg.robolectric:robolectric:3.0 RC3
testCompile'的JUnit:JUnit的:4.12
testCompileorg.robolectric:阴影-multidex:3.0
}

下面是我的单元测试,

  @RunWith(RobolectricGradleTestRunner.class)
@Config(常量= BuildConfig.class,SDK = Build.VERSION_ codeS.LOLLIPOP,阴影= {} ShadowMultiDex.class)
公共类MyActivityTest {
    活动活动= NULL;
    @之前
    公共无效的init(){
    }
    @测试
    公共无效getMillisecondFromHourTest(){
    }
}

如果我运行单元测试与其中 ActiveAndroid.initialize(本)的自定义应用程序类; 在不被称为我的单元测试运行(仅适用于那些静态断言)。

如果我使用一个应用程序类调用 ActiveAndroid.initialize(本); 发生这种情况:

 显示java.lang.NullPointerException
    在dalvik.system.DexFile $ DFEnum.hasMoreElements(DexFile.java:262)
    在com.activeandroid.ReflectionUtils.getParsers(ReflectionUtils.java:152)
    在com.activeandroid.Registry.initialize(Registry.java:100)
    在com.activeandroid.ActiveAndroid.initialize(ActiveAndroid.java:8)
    在com.activeandroid.app.Application.onCreate(Application.java:9)
    在com.MyApplication.onCreate(MyApplication.java:28 1)
    在org.robolectric.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:140)
    在org.robolectric.RobolectricTestRunner.setUpApplicationState(RobolectricTestRunner.java:433)
    在org.robolectric.RobolectricTestRunner $ 2.evaluate(RobolectricTestRunner.java:240)
    在org.robolectric.RobolectricTestRunner.runCh​​ild(RobolectricTestRunner.java:188)
    在org.robolectric.RobolectricTestRunner.runCh​​ild(RobolectricTestRunner.java:54)
    在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:290)
    在org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)
    在org.junit.runners.ParentRunner.runCh​​ildren(ParentRunner.java:288)
    在org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)
    在org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268)
    在org.robolectric.RobolectricTestRunner $ 1.evaluate(RobolectricTestRunner.java:152)
    在org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    在org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    在com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
    在com.intellij.rt.execution.junit.JUnitStarter。prepareStreamsAndStart(JUnitStarter.java:212)
    在com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
    在com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)了java.lang.RuntimeException:显示java.lang.NullPointerException
    在org.robolectric.RobolectricTestRunner $ 2.evaluate(RobolectricTestRunner.java:244)
    在org.robolectric.RobolectricTestRunner.runCh​​ild(RobolectricTestRunner.java:188)
    在org.robolectric.RobolectricTestRunner.runCh​​ild(RobolectricTestRunner.java:54)
    在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:290)
    在org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)
    在org.junit.runners.ParentRunner.runCh​​ildren(ParentRunner.java:288)
    在org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)
    在org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268)
    在org.robolectric.RobolectricTestRunner $ 1.evaluate(RobolectricTestRunner.java:152)
    在org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    在org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    在com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
    在com.intellij.rt.execution.junit.JUnitStarter。prepareStreamsAndStart(JUnitStarter.java:212)
    在com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
    在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)
    在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    在com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
显示java.lang.NullPointerException:产生的原因
    在dalvik.system.DexFile $ DFEnum.hasMoreElements(DexFile.java:262)
    在com.activeandroid.ReflectionUtils.getParsers(ReflectionUtils.java:152)
    在com.activeandroid.Registry.initialize(Registry.java:100)
    在com.activeandroid.ActiveAndroid.initialize(ActiveAndroid.java:8)
    在com.activeandroid.app.Application.onCreate(Application.java:9)
    在com.MyApplication.onCreate(MyApplication.java:281)
    在org.robolectric.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:140)
    在org.robolectric.RobolectricTestRunner.setUpApplicationState(RobolectricTestRunner.java:433)
    在org.robolectric.RobolectricTestRunner $ 2.evaluate(RobolectricTestRunner.java:240)
    在org.robolectric.RobolectricTestRunner.runCh​​ild(RobolectricTestRunner.java:188)
    在org.robolectric.RobolectricTestRunner.runCh​​ild(RobolectricTestRunner.java:54)
    在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:290)
    在org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)
    在org.junit.runners.ParentRunner.runCh​​ildren(ParentRunner.java:288)
    在org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)
    在org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268)
    在org.robolectric.RobolectricTestRunner $ 1.evaluate(RobolectricTestRunner.java:152)
    在org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    在org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    在com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
    ... 1更多

顺便说一句。我有这个在我的MyApplication.class还

  @覆盖
保护无效attachBaseContext(上下文基地){
    super.attachBaseContext(基峰);
    MultiDex.install(本);
}


解决方案

啊,我们也有类似的问题。我们在明年的 Robolectric http://robolectric.org/custom-测试亚军/

我们感动创建方式:

  @覆盖
保护无效attachBaseContext(上下文基地){
    super.attachBaseContext(基峰);
    inatallMultiDex();
}保护无效installMultiDex(){
    MultiDex.install(本);
}

和在测试应用程序overrided吧:

  @覆盖
保护无效installMultiDex(){
    //什么都不做
}

I am trying to implement robolectric testing in an existing app. I can run simple static tests but when I add testing with ActiveAndroid I get NullPointerExceptions, I think ActiveAndroid can not be found when the unit test is being run.

Im not sure maybe there are some configurations for ActiveAndroid for Robelectric unit testing. Kindly help me on this one.

This is the project structure:

MainApp
-src/main
-src/test

LibraryApp
-src/main/models <--- here are my models and classes for active android
-libs/activeandroid-3.0.jar <---- the active android jar file

I imported ActiveAndroid-3.0.jar in the Library App.

Here are the build.gradle files:

MainApp build.gradle

android {
compileSdkVersion 21
buildToolsVersion "22.0.0"

defaultConfig {
    applicationId "com....."
    minSdkVersion 18
    targetSdkVersion 21
    versionCode 1
    versionName "1.0"

    // Enabling multidex support.
    multiDexEnabled true

    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

dexOptions {
    preDexLibraries = false
}

sourceSets {
    main {
        java {
            exclude 'com/google/gson/annotations/Expose.class'
        }
    }
}

lintOptions{
    abortOnError false
}
packagingOptions{
    exclude 'META-INF/NOTICE.txt'
    exclude 'META-INF/LICENSE.txt'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile files('libs/gcm.jar')
compile 'com.android.support:multidex:1.0.1'
compile 'joda-time:joda-time:2.9.1'
androidTestCompile 'junit:junit:4.12'
testCompile 'junit:junit:4.12'
compile 'com.facebook.fresco:fresco:0.7.0+'
compile 'com.github.bluejamesbond:textjustify-android:2.1.0'
compile files('libs/activeandroid-3.0.jar')
compile 'com.google.android.gms:play-services:7.5.0'
compile 'com.jakewharton:butterknife:7.0.1'
compile files('libs/FlurryAnalytics-5.5.0.jar')
compile project(':app')
androidTestCompile 'com.android.support.test:runner:0.3'
testCompile 'com.android.support.test:runner:0.3'

androidTestCompile 'com.android.support.test:rules:0.3'
testCompile 'com.android.support.test:rules:0.3'
// Set this dependency to build and run Espresso tests
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2'
// Set this dependency to build and run UI Automator tests
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'
//compile 'com.squareup.retrofit:retrofit:2.0.0-beta1'

//classpath 'com.stanfy.spoon:spoon-gradle-plugin:1.0.2'
compile 'com.github.jakob-grabner:Circle-Progress-View:v1.2.2'
compile('com.crashlytics.sdk.android:crashlytics:2.5.5@aar') {
    transitive = true;
}
testCompile "org.robolectric:robolectric:3.0-rc3"
testCompile 'junit:junit:4.12'
testCompile "org.robolectric:shadows-multidex:3.0"
}

LibraryApp build.gradle

apply plugin: 'com.android.library'

android {
compileSdkVersion 21
buildToolsVersion "22.0.0"

defaultConfig {
    minSdkVersion 18
    targetSdkVersion 22
    versionCode 1
    versionName "1.0"

    // Enabling multidex support.
    multiDexEnabled false
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
dexOptions {
    preDexLibraries = false
}

packagingOptions{
    exclude 'META-INF/NOTICE.txt'
    exclude 'META-INF/LICENSE.txt'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/NOTICE'
}

configure(android.lintOptions) {
    abortOnError false
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])

compile 'com.balysv.materialmenu:material-menu-toolbar:1.5.4'
compile 'org.lucasr.twowayview:twowayview:0.1.4'
compile files('libs/gson-2.3.1.jar')
compile 'org.apache.httpcomponents:httpmime:4.2.3'
compile files('libs/activeandroid-3.0.jar')
compile 'com.google.code.gson:gson:2.3.1'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.scottyab:aescrypt:0.0.1'
compile 'com.android.support:multidex:1.0.1'

testCompile "org.robolectric:robolectric:3.0-rc3"
testCompile 'junit:junit:4.12'
testCompile "org.robolectric:shadows-multidex:3.0"
}

Here is my unit test,

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = Build.VERSION_CODES.LOLLIPOP, shadows= {ShadowMultiDex.class})
public class MyActivityTest {
    Activity activity = null;
    @Before
    public void init(){
    }
    @Test
    public void getMillisecondFromHourTest(){
    }
}

If I run the unit test with a custom Application class where ActiveAndroid.initialize(this); where is not being called my unit tests run (only those static asserts).

If I use an Application class the calls ActiveAndroid.initialize(this); this happens:

    java.lang.NullPointerException
    at dalvik.system.DexFile$DFEnum.hasMoreElements(DexFile.java:262)
    at com.activeandroid.ReflectionUtils.getParsers(ReflectionUtils.java:152)
    at com.activeandroid.Registry.initialize(Registry.java:100)
    at com.activeandroid.ActiveAndroid.initialize(ActiveAndroid.java:8)
    at com.activeandroid.app.Application.onCreate(Application.java:9)
    at      com.MyApplication.onCreate(MyApplication.java:28 1)
    at org.robolectric.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:140)
    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: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.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 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: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.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:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.lang.NullPointerException
    at dalvik.system.DexFile$DFEnum.hasMoreElements(DexFile.java:262)
    at com.activeandroid.ReflectionUtils.getParsers(ReflectionUtils.java:152)
    at com.activeandroid.Registry.initialize(Registry.java:100)
    at com.activeandroid.ActiveAndroid.initialize(ActiveAndroid.java:8)
    at com.activeandroid.app.Application.onCreate(Application.java:9)
    at com.MyApplication.onCreate(MyApplication.java:281)
    at org.robolectric.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:140)
    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: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.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
    ... 1 more

By the way. I have this in my MyApplication.class also

@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);
    MultiDex.install(this);
}

解决方案

Ah, we had a similar issue. We resolved it by next Robolectric feature http://robolectric.org/custom-test-runner/

We moved created method:

@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);
    inatallMultiDex();
}

protected void installMultiDex() {
    MultiDex.install(this);
}

And overrided it in the test application:

@Override
protected void installMultiDex() {
    //don't do anything
}

这篇关于Robolectric与ActiveAndroid $ ReflectionUtils ActiveAndroid设置NullPointerException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆