Aggiornamento 1Impossibile utilizzare FakeHttpLayer di Robolectric (NullPointerException quando si chiama getFakeHttpLayer)
Dopo aver rimosso la ServiceTestCase
estendono della mia classe di test, ho modificato il mio file Gradle per cambiare il testInstrumentationRunner
a org.robolectric.RobolectricTestRunner
ma ottengo un altro errore:
Running tests
Test running started
Test running failed: Instrumentation run failed due to 'java.lang.NoSuchMethodException'
Empty test suite.
Ho cercato su Google ma non sono riuscito a scoprire perché ho ricevuto questo messaggio di errore. Qui è l'intero file 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:[email protected]'
androidTestCompile 'com.jayway.android.robotium:robotium-solo:5.2.1'
androidTestCompile 'org.robolectric:robolectric:2.4'
androidTestCompile 'junit:junit:4.12'
}
Fine dell'aggiornamento 1
Sto cercando di utilizzare Robolectric 2.4 per testare le chiamate di rete della mia biblioteca Android. Il problema è che non riesco nemmeno ad accedere a FakeHttpLayer di Robolectric senza ottenere 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)
C'è la mia classe di prova:
@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();
}
}
mi manca un po 'di configurazione di base che deve essere impostata su Robolectric prima di essere in grado di usarlo? Non riesco a trovare alcun buon tutorial che mi aiuti a capire cosa ho fatto di sbagliato.
Grazie
Hai trovato qualche soluzione? – Dory
No, ho rinunciato a Robolectric. –
Quindi hai qualche altro framework per le chiamate a riposo? Puoi per favore guidarmi? – Dory