正如标题所说,使用 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)
有任何想法吗?