2013-08-22 104 views
5

Robolectric和程序兼容性結果正如標題所說運行Robolectric測試與搖籃導致NullPointerException同時呼籲Robolectric.buildActivity(MyActivity.class).create().get();運行搖籃測試與NullPointerException異常

的build.gradle(基於this answer

buildscript { 
    repositories { 
     mavenCentral() 
    } 
    dependencies { 
     classpath 'com.android.tools.build:gradle:0.5.+' 
    } 
} 

repositories { 
    mavenCentral() 
    maven { 
     url 'https://oss.sonatype.org/content/repositories/snapshots/' 
    } 
} 

apply plugin: 'android' 

android { 
    compileSdkVersion 18 
    buildToolsVersion "18.0.1" 

    defaultConfig { 
     minSdkVersion 7 
     targetSdkVersion 16 
    } 
} 

sourceSets { 
    testLocal { 
     java.srcDir file('src/test/java') 
     resources.srcDir file('src/test/resources') 
    } 
} 

dependencies { 
    compile 'com.android.support:support-v4:18.0.0' 
    compile 'com.android.support:appcompat-v7:18.0.+' 
    compile project(':Libraries:ViewPagerIndicator') 

    testLocalCompile project(':Libraries:appcompat') 
    testLocalCompile 'junit:junit:4.8.2' 
    testLocalCompile 'org.robolectric:robolectric:2.1' 
    testLocalCompile 'com.google.android:android:4.1.1.4' 
    testLocalCompile 'com.google.android:support-v4:r6' 
    testLocalCompile 'org.roboguice:roboguice:2.0' 
} 

task localTest(type: Test, dependsOn: assemble) { 
    testClassesDir = sourceSets.testLocal.output.classesDir 

    android.sourceSets.main.java.srcDirs.each { dir -> 
     def buildDir = dir.getAbsolutePath().split('/') 
     buildDir = (buildDir[0..(buildDir.length - 4)] + ['build', 'classes', 'debug']).join('/') 

     sourceSets.testLocal.compileClasspath += files(buildDir) 
     sourceSets.testLocal.runtimeClasspath += files(buildDir) 
    } 

    classpath = sourceSets.testLocal.runtimeClasspath 
} 

check.dependsOn localTest 

MyActivityTest.java

@RunWith(RobolectricTestRunner.class) 
@Config(manifest=Config.NONE) 
public class MyActivityTest { 

    MyActivity activity; 


    @Before 
    public void setUp() { 
     activity = Robolectric.buildActivity(MyActivity.class).create().get(); 
    } 

    @Test 
    public void testSomeNumber() { 
     Assert.assertEquals(true,true); 
    } 
} 

MyActivity.java延伸,其延伸ActionBarActivity

運行命令行這種配置主叫./gradlew localTest結果在下面的例外

堆棧跟蹤

java.lang.NullPointerException 
    at org.robolectric.res.builder.RobolectricPackageManager.getActivityInfo(RobolectricPackageManager.java:62) 
    at android.support.v7.app.ActionBarActivityDelegate.getUiOptionsFromMetadata(ActionBarActivityDelegate.java:157) 
    at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:53) 
    at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98) 
    at com.example.activity.BaseActivity.onCreate(BaseActivity.java:17) 
    at com.example.activity.MyActivity.onCreate(MyActivity.java:27) 
    at android.app.Activity.performCreate(Activity.java:5008) 
    at org.fest.reflect.method.Invoker.invoke(Invoker.java:112) 
    at org.robolectric.util.ActivityController$1.run(ActivityController.java:119) 
    at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:256) 
    at org.robolectric.util.ActivityController.create(ActivityController.java:114) 
    at org.robolectric.util.ActivityController.create(ActivityController.java:126) 
    at com.example.test.MyActivityTest.setUp(MyActivityTest.java:35) 

任何想法一個BaseActivity?

+0

好像問題出在MyActivity – Selvin

+0

是啊,好像是。但該行只是調用'super.onCreate(savedInstanceState);'與BaseActivity相同,在此之前我不會調用任何東西。 – dng

+1

我不確定,但從文檔'Config.NONE'如果你不使用資源或項目沒有清單。但這兩種情況都不適用於活動測試 –

回答

0

嘗試更改您的構建腳本。當你執行構建任務時,gradle文件下面會自動運行robolectric測試。您可以根據需要編輯它。我們不得不使用快照版本。 更多詳細信息:gradle-android-test-plugin

測試的默認路徑是

的src /測試/ JAVA/...

buildscript { 
    repositories { 
    mavenCentral() 
    maven { 
     url 'https://oss.sonatype.org/content/repositories/snapshots/' 
    } 
    } 
    dependencies { 
    classpath 'com.android.tools.build:gradle:0.6.+' 
    classpath 'com.squareup.gradle:gradle-android-test-plugin:0.9.1-SNAPSHOT' 

    } 
} 
apply plugin: 'android' 
apply plugin: 'android-test' 

repositories { 
mavenCentral() 
} 

android { 
compileSdkVersion 19 
buildToolsVersion "19.0.0" 

defaultConfig { 
    minSdkVersion 8 
    targetSdkVersion 19  
} 
} 

dependencies { 
    compile 'com.android.support:appcompat-v7:+' 
    testCompile 'junit:junit:4.10' 
    testCompile 'org.robolectric:robolectric:2.1.+' 
    testCompile 'com.squareup:fest-android:1.0.+' 
}