5

正如标题所说,使用 Gradle 运行 Robolectric 测试会导致NullPointerException调用Robolectric.buildActivity(MyActivity.class).create().get();

build.gradle(基于这个答案

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 扩展了一个 BaseActivity,它扩展了 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)

有任何想法吗?

4

1 回答 1

0

尝试更改您的构建脚本。当您执行构建任务时,下面的 gradle 文件将自动运行 robolectric 测试。您可以根据需要编辑它。

当前版本的 android-test-plugin 存在问题,因此我们必须使用快照版本。更多细节:gradle-android-test-plugin

测试的默认路径是

src/test/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.+'
}
于 2013-12-27T06:00:22.313 回答