2016-08-01 16 views
1

Ich habe einen Instrumental-Test bei app/androidTest/java/myapp. Ich habe eine einfache Testmethode dort:Instrumententests - Nullzeiger Ausnahme auf SetContentView()

public class LoginActivityTest { 

    private static final String EMAIL = "[email protected]"; 
    private static final String PASSWORD = "admin"; 

    @Rule 
    public IntentsTestRule<LoginActivity> loginActivity = new IntentsTestRule<>(LoginActivity.class); 

    @Test 
    public void LoginWithProperCredentials() { 
     onView(withId(R.id.email_textView)) 
       .perform(typeText(EMAIL)); 
     onView(withId(R.id.password_login)) 
       .perform(typeText(PASSWORD)); 
     onView(withId(R.id.email_sign_in_button)) 
       .perform(click()); 

     intended(allOf(
       hasExtra(GeneralConstants.AUTHENTICATED, true), 
       hasExtra(GeneralConstants.TOKEN, isA(String.class)), 
       toPackage("myapp"))); 
    } 
} 

I Android Support-Repository aufgenommen notwendig androidTestCompile Abhängigkeiten sowie Standard testInstrumentationRunner Upgrade ("android.support.test.runner.AndroidJUnitRunner") und ich habe Android Instrumentation Tests gewählt als Variant bauen. Der Versuch, den Test zu starten, gibt mir jedoch ResourceNotFoundException. Ich habe herausgefunden, dass es die setContentView() Methode in onCreate(Bundle bundle) ist, die diese Ausnahme auslöst.

Ich habe damit schon eine Weile zu kämpfen. Was könnte der Grund dafür sein?

EDIT:

ich überprüft haben, dass in myapp.test.R dort ein fehlendes Feld, das ResourceNotFoundException verursacht. Dieses Feld ist auch in myapp.R, aber es hat anderen Wert zugeordnet (0x7f040015 und 0x7f030015 in test.R -dieser fehlt). Ich habe versucht, explizit importieren:

import pl.kawowydzienniczek.kawowydzienniczek.test.R; 

Aber dann versucht, schreiben:

onView(withId(R.id.email_textView)) 
       .perform(typeText(EMAIL)); 

gibt mir die Fehlermeldung, dass email_textView nicht aufgelöst werden kann. Wie soll es gemacht werden?

EDIT2:

Ich habe bemerkt, dass die folgende Abhängigkeit

androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.2'

macht Auskommen es funktionieren. Für jetzt sollte es okay sein, aber warum ist das so und wie macht man Espresso, ohne diese Bibliothek auszuwerfen?

+0

nur neugierig, aber haben Sie mehrere Ressourcendateien für diese Tätigkeit? – dungtatas

+0

Was meinen Sie mit mehreren Ressourcen? Ich habe einen 'res'-Ordner und einen' layout'-Ordner darin. –

+0

Hat diese Aktivität mehrere Layouts? Zum Beispiel Layout und Layout-Land. Aber von deiner letzten Antwort klingt es nicht so. Ich hatte Probleme, bei denen ich mehrere Layouts mit unterschiedlichen Ansichtselementen hatte, die ein ähnliches Problem verursachten, das Sie beschrieben haben. – dungtatas

Antwort

0

Nach

Vorerst sollte es okay sein, aber warum ist das so und wie Espresso Arbeit zu machen, ohne dass die Bibliothek heraus zu werfen?

hier meine build.gradle config:

dependencies { 
    ext.JUNIT_VERSION = '4.12' 
    ext.SUPPORT_VERSION = '24.1.1' 
    ext.ESPRESSO_VERSION = '2.2.2' 

    compile fileTree(dir: ulibs', include: ['*.jar']) 
    testCompile "junit:junit:$JUNIT_VERSION" 
    testCompile("org.robolectric:robolectric:3.0") { 
     exclude module: 'classworlds' 
     exclude module: 'commons-logging' 
     exclude module: 'httpclient' 
     exclude module: 'maven-artifact' 
     exclude module: 'maven-artifact-manager' 
     exclude module: 'maven-error-diagnostics' 
     exclude module: 'maven-model' 
     exclude module: 'maven-project' 
     exclude module: 'maven-settings' 
     exclude module: 'plexus-container-default' 
     exclude module: 'plexus-interpolation' 
     exclude module: 'plexus-utils' 
     exclude module: 'wagon-file' 
     exclude module: 'wagon-http-lightweight' 
     exclude module: 'wagon-provider-api' 
    } 

    compile "com.android.support:appcompat-v7:$SUPPORT_VERSION" 
    compile "com.android.support:design:$SUPPORT_VERSION" 

    androidTestCompile 'com.android.support.test:runner:0.5' 
    androidTestCompile "com.android.support:support-annotations:$SUPPORT_VERSION" 
    androidTestCompile "com.android.support.test.espresso:espresso-core:$ESPRESSO_VERSION" 
    androidTestCompile "com.android.support.test.espresso:espresso-intents:$ESPRESSO_VERSION" 
    /** 
    * AccessibilityChecks 
    * CountingIdlingResource 
    * DrawerActions 
    * DrawerMatchers 
    * PickerActions (Time and Date picker) 
    * RecyclerViewActions 
    */ 
    androidTestCompile("com.android.support.test.espresso:espresso-contrib:$ESPRESSO_VERSION") { 
     exclude group: 'com.android.support', module: 'appcompat' 
     exclude group: 'com.android.support', module: 'support-v4' 
     exclude group: 'com.android.support', module: 'support-v7' 
     exclude group: 'com.android.support', module: 'design' 
     exclude module: 'support-annotations' 
     exclude module: 'recyclerview-v7' 
    } 
    compile "junit:junit:${JUNIT_VERSION}" 
} 

versuchen, die gleichen Gruppen Module und Gruppen ausschließen, wie ich bereits getan habe.

Check: https://github.com/piotrek1543/LocalWeather/blob/master/app/build.gradle

Hoffnung wird es helfen,

Verwandte Themen