正如标题所说,在调用NullPointerException
Robolectric.buildActivity(MyActivity.class).create().get();
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
的BaseActivity从命令行调用./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)
有什么想法吗?
答案 0 :(得分:0)
尝试更改构建脚本。执行构建任务时,gradle文件下面会自动运行robolectric测试。您可以根据需要进行编辑。
当前版本的android-test-plugin存在问题,因此我们必须使用快照版本。 更多详情: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.+'
}