Cannot use FakeHttpLayer Robolectric (NullPointerException exception when calling getFakeHttpLayer)

Update 1

After uninstalling the ServiceTestCase extension of my test class, I edited the gradle file to change 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 am getting 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 access the FakeHttpLayer 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 class 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(); } } 

Is there any basic configuration Robolectric needs to install before using it? I can’t find a good tutorial to help me understand what I did wrong.

thanks

+6
source share
2 answers

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

Roboelectric tests are performed on the JVM, not on the device. Android device tests were run on the device.

You can also check this from the point of view of testing your service: http://robolectric.org/starting-components/

+2
source

This is probably because you are extending it from the ServiceTestCase class. Do not use the Android Unit Test package for this. Also make sure you create this class on JUnit 4.

Another thing is you must build a service with Robolectric. Like this:

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

EDIT: add these fields to the build.gradle file, this is from the 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' } 
+1
source

Source: https://habr.com/ru/post/982467/


All Articles