Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot use FakeHttpLayer of Robolectric (NullPointerException when calling getFakeHttpLayer)

Update 1

After removing the ServiceTestCase extend of my test class, I edited my gradle file to change the testInstrumentationRunner to org.robolectric.RobolectricTestRunner but I get another error :

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

I searched Google but I could not find out why I get this error message. Here is the whole gradle file :

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'
}

End of update 1


I am trying to use Robolectric 2.4 to test the network calls of my Android Library. The problem is that I can't even get access to the FakeHttpLayer of Robolectric without getting a 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)

There is my Test class :

@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();
    }

}

Am I missing some basic configuration that must be set to Robolectric before being able to use it? I can't find any good tutorial that would help me to understand what I did wrong.

Thank you

like image 822
Raphael Royer-Rivard Avatar asked Feb 13 '15 21:02

Raphael Royer-Rivard


2 Answers

Not sure if this is why you have this specific problem, but one issue is that you are extending a ServiceTestCase. This is a problem, because you are now mixing Robolectric with Android Unit Tests.

Robolectric tests run on the JVM, not on device. Android Unit Tests ran on device.

You might also want to check this out in terms of how to test your service: http://robolectric.org/starting-components/

like image 86
Alex Florescu Avatar answered Oct 25 '22 20:10

Alex Florescu


Probably it's because you are extending it from ServiceTestCase class. Don't use Android Unit Test suite for that. Also be sure you have created that class on JUnit 4.

Another thing is you should build the service with Robolectric way. Like this;

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

EDIT : Add these fields to your build.gradle file, these are from 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'
}
like image 28
Deliganli Avatar answered Oct 25 '22 21:10

Deliganli