不能使用Robolectric的FakeHttpLayer(调用getFakeHttpLayer时为NullPointerException)

时间:2015-02-13 21:15:11

标签: java android robolectric

更新1

删除我的测试类的ServiceTestCase扩展后,我编辑了我的gradle文件,将testInstrumentationRunner更改为org.robolectric.RobolectricTestRunner,但我收到了另一个错误:

Running tests
Test running started
Test running failed: Instrumentation run failed due to 'java.lang.NoSuchMethodException'
Empty test suite.

我搜索了Google,但我找不到为什么会收到此错误消息。这是整个gradle文件:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 19
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "foo.bar.testappsdk"
        minSdkVersion 9
        targetSdkVersion 19

        testApplicationId "novom.anyware.anywaresdk.test"
        testInstrumentationRunner "org.robolectric.RobolectricTestRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }

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

    sourceSets {
        instrumentTest.setRoot('src/androidTest')
    }
}

dependencies {
    compile 'com.android.support:support-v4:19.1.0'
    compile 'com.android.support:appcompat-v7:19.1.0'
    compile 'com.google.android.gms:play-services:6.5.87'
    compile 'novom.anyware.anywaresdk:anywaresdk@aar'
    androidTestCompile 'com.jayway.android.robotium:robotium-solo:5.2.1'
    androidTestCompile 'org.robolectric:robolectric:2.4'
    androidTestCompile 'junit:junit:4.12'
}

更新结束1


我正在尝试使用Robolectric 2.4来测试我的Android库的网络调用。问题是我甚至无法访问Robolectric的FakeHttpLayer而不会得到NullPointerException。

java.lang.NullPointerException: Attempt to invoke virtual method 'org.robolectric.tester.org.apache.http.FakeHttpLayer org.robolectric.shadows.ShadowApplication.getFakeHttpLayer()' on a null object reference
at org.robolectric.Robolectric.getFakeHttpLayer(Robolectric.java:1239)
at novom.anyware.anywaresdk.test.AWRSyncServiceTest.test_3_get_config(AWRSyncServiceTest.java:61)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1837)

有我的Test类:

@RunWith(RobolectricTestRunner.class)
public class AWRSyncServiceTest extends ServiceTestCase<AWRSyncService> {
    private static final String TAG = "AWRSyncServiceTest";

    private AWRSyncService syncService;

    public AWRSyncServiceTest() {
        super(AWRSyncService.class);
    }

    @Override
    protected void setUp() throws Exception {
        Log.d(TAG, "setUp");
        super.setUp();
        IBinder service = bindService(new Intent(getContext(), AWRSyncService.class));
        AWRSyncService.SyncServiceBinder binder = (AWRSyncService.SyncServiceBinder) service;
        syncService = binder.getService();
    }

    @Override
    protected void tearDown() throws Exception {
        Log.d(TAG, "tearDown");
        super.tearDown();
    }

    public void test_1_preconditions() {
        Log.d(TAG, "test_1_preconditions");
        assertNotNull(syncService);
        assertNotNull(syncService.getApplicationContext());
    }

    public void test_3_get_config() {
        Robolectric.getFakeHttpLayer();
    }

}

我是否缺少一些必须设置为Robolectric的基本配置才能使用它?我找不到任何可以帮助我理解错误的教程。

谢谢

2 个答案:

答案 0 :(得分:2)

不确定这是否是您遇到此特定问题的原因,但有一个问题是您正在扩展ServiceTestCase。这是一个问题,因为您现在正在将Robolectric与Android单元测试混合使用。

Robolectric测试在JVM上运行,而不是在设备上运行。 Android设备测试在设备上运行。

您可能还希望在如何测试服务方面进行检查:http://robolectric.org/starting-components/

答案 1 :(得分:1)

可能是因为你是从ServiceTestCase类扩展它。不要使用Android Unit Test套件。还要确保在JUnit 4上创建了该类。

另一件事是你应该用Robolectric方式构建服务。像这样;

Robolectric.buildService(AWRSyncService.class).bind()

编辑:将这些字段添加到build.gradle文件中,这些文件来自robolectric gradle plugin

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath 'org.robolectric:robolectric-gradle-plugin:0.13.2'
    }
}

apply plugin: 'robolectric'

android {
sourceSets {
    androidTest {
        setRoot('src/androidTest')
    }
}

robolectric {
    include '**/*Test.class'
}