10

Sto provando a scrivere Casi di test unità per Attività nella mia app estendendo la classe di test con ActivityUnitTestCase. Potrei eseguire correttamente i test case in precedenza, ma ora ottengo sempre l'eccezione durante l'esecuzione. Anche se ho una certa familiarità con la gestione di NullPointerExceptions, non sono riuscito a capire il problema che sta causando questo. Non sono riuscito a trovare domande simili, quindi sto pubblicando questo.Errore durante l'esecuzione della strumentazione a causa di 'java.lang.NullPointerException'

traccia Pila mi mostra v'è un riferimento oggetto null a questa riga nel mio codice

activity = startActivity(mIntent, null, null); 

Ma il metodo startActivity dovrebbe ottenere l'istanza dell'attività sto testando. Non sono sicuro del motivo per cui restituisce null.

Ecco la traccia dello stack.

java.lang.NullPointerException: Attempt to write to field 'android.os.IBinder android.app.ActivityThread.mLastIntendedActivityToken' on a null object reference 
at android.app.Activity.performCreate(Activity.java:6372) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) 
at android.support.test.runner.MonitoringInstrumentation.callActivityOnCreate(MonitoringInstrumentation.java:346) 
at android.test.ActivityUnitTestCase.startActivity(ActivityUnitTestCase.java:158) 
at com.abc.test.MainActivityTest.access$100(MainActivityTest.java:16) 
at com.abc.test.MainActivityTest$1.run(MainActivityTest.java:34) 
at android.app.Instrumentation$SyncRunnable.run(Instrumentation.java:1891) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:145) 
at android.app.ActivityThread.main(ActivityThread.java:6117) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) 

Test running failed: Instrumentation run failed due to 'java.lang.NullPointerException' 

Ecco la classe Test

public class MainActivityTest extends ActivityUnitTestCase<MainActivity>{ 

    private Intent mIntent; 
    private MainActivity activity; 

    public MainActivityTest() { 
     super(MainActivity.class); 
    } 

    @Override 
    protected void setUp() throws Exception { 
     super.setUp(); 
     //Create an intent to launch target Activity as it is not automatically started by Android Instrumentation 
     mIntent = new Intent(getInstrumentation().getContext(), MainActivity.class); 
     //Start the activity under test in isolation, in the main thread to avoid assertion error. 
     getInstrumentation().runOnMainSync(new Runnable() { 
      @Override 
      public void run() { 
       activity = startActivity(mIntent, null, null); 
      } 
     }); 
    } 

    @Override 
    protected void tearDown() throws Exception { 
     super.tearDown(); 
    } 

    /** 
    * Tests the preconditions of this test fixture. 
    */ 
    @SmallTest 
    public void testPreconditions() { 
     assertNotNull("MainActivity is null", getActivity()); 
    } 

    @MediumTest 
    public void testSecondActivityWasLaunchedWithIntent() { 
     // Get the intent for the next started activity 
     final Intent launchIntent = getStartedActivityIntent(); 
     //Verify the intent was not null. 
     assertNotNull("Intent was null", launchIntent); 
     //Verify that LaunchActivity was finished 
     assertTrue(isFinishCalled()); 
    } 
} 
+1

@Marcin Questo non è un mero duplicato. Ho provato a eseguire il debug ma non ho trovato indizi e non ho trovato nessuna domanda correlata. Quindi ne ho pubblicato uno. – Prudhvi

+1

Sono d'accordo, questo non è un semplice caso NullPointerException. L'eccezione viene generata dall'interno della catena di chiamate della classe ActivityUnitTestCase di Android. –

+0

@prudnvi Ho ottenuto la stessa eccezione in un caso di test simile che ho scritto. Stavo eseguendo il test su un dispositivo Lollipop. Ho appena eseguito il test su KitKat e il test è stato eseguito correttamente. Che test runner stai usando? Sto usando GoogleInstrumentationTestRunner. Mi chiedo se dobbiamo eseguire l'aggiornamento al nuovo runner di test: AndroidJUnitRunner, che è nelle versioni più recenti dell'SDK. –

risposta

0

@prudhvi io non credo di avere un proiettile d'argento, purtroppo, ma vorrei suggerire cercando di seguire these steps per l'aggiornamento alle nuove librerie di supporto di prova nelle versioni più recenti dell'SDK. Mi dispiace di non poter essere più d'aiuto!

0

Ho avuto lo stesso problema. La soluzione era di fare il test di un ActivityInstrumentationTestCase2 invece di un ActivityUnitTestCase, e hanno l'attività creata per me in background

0

C'è una buona probabilità qualcosa nella tua classe di applicazione (cioè X estende Application) si blocca nella fase iniziale. Se questo è il caso, vedrai questo errore .... Controlla il tuo logcat per trovare tracce.

0

ho cambiato ActivityUnitTestCase a ActivityInstrumentationTestCase2 e rimosso il mio startActivity() chiamate (sembra che ActivityInstrumentationTestCase2 avvia automaticamente l'attività) ora si corre di nuovo

Problemi correlati