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
source share