Ich versuche, einen Espresso-UI-Test innerhalb des neuen Android-Projekts zu erstellen, aber ich hatte folgendes Problem.
Wenn ich versucht habe, eine leere Testklasse zu erstellen:
import Android.content.Intent;
import Android.support.test.rule.ActivityTestRule;
import Android.support.test.runner.AndroidJUnit4;
import Android.test.ActivityInstrumentationTestCase2;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import static Android.support.test.espresso.Espresso.onView;
import static Android.support.test.espresso.assertion.ViewAssertions.matches;
import static Android.support.test.espresso.matcher.ViewMatchers.withId;
import static Android.support.test.espresso.matcher.ViewMatchers.withText;
@RunWith(AndroidJUnit4.class)
public class LoginActivityTest extends ActivityInstrumentationTestCase2<LoginActivity> {
}
Ich bekomme immer diese Fehlermeldung:
cannot resolve symbol AndroidJUnit4.class
Und fast alle importierten Bibliotheken werden als nicht verwendet markiert.
die build.gradle-Datei enthält Folgendes:
apply plugin: 'com.Android.application'
Android {
compileSdkVersion 23
buildToolsVersion "23.0.0"
defaultConfig {
applicationId "com.some.thing.xxx"
minSdkVersion 14
targetSdkVersion 23
versionCode 1
versionName "1.0"
testInstrumentationRunner "Android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-Android.txt'), 'proguard-rules.pro'
}
}
lintOptions {
abortOnError false
}
packagingOptions {
exclude 'LICENSE.txt'
}
}
repositories {
mavenCentral()
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
maven { url "https://jitpack.io" }
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.Android.support:appcompat-v7:23.0.0'
compile 'com.google.Android.gms:play-services:7.8.0'
compile 'com.mcxiaoke.volley:library:1.0.18'
compile 'com.orhanobut:logger:1.11'
// App dependencies
compile 'com.Android.support:support-annotations:23.0.0'
// TESTING DEPENDENCIES
androidTestCompile 'com.Android.support.test:runner:0.3'
// Set this dependency to use JUnit 4 rules
androidTestCompile 'com.Android.support.test:rules:0.3'
// Set this dependency to build and run Espresso tests
androidTestCompile 'com.Android.support.test.espresso:espresso-core:2.2'
// add this for intent mocking support
androidTestCompile 'com.Android.support.test.espresso:espresso-intents:2.2'
// add this for webview testing support
androidTestCompile 'com.Android.support.test.espresso:espresso-web:2.2'
// Set this dependency to build and run UI Automator tests
androidTestCompile 'com.Android.support.test.uiautomator:uiautomator-v18:2.1.1'
androidTestCompile 'com.Android.support.test.espresso:espresso-contrib:2.2'
}
Wenn ich diese Einstellung für ein anderes Testprojekt verwende, funktioniert es, sodass ich nicht weiß, was falsch sein kann.
Ich bin diesem Tutorial gefolgt: "
http://www.vogella.com/tutorials/AndroidTestingEspresso/article.html
Und ich habe versucht, das Problem zu lösen, indem Sie die SO - Frage befolgen: Symbol 'AndroidJUnit4' kann nicht aufgelöst werden
Aber ohne das Glück.
Vielen Dank für jeden Hinweis.
Ich habe dasselbe Tutorial auch von Vogella ausprobiert und bin auf viele Probleme gestoßen. Eines der ersten Probleme, auf das ich stieß, war ein Abhängigkeitskonflikt zwischen den Anmerkungsversionen von v23-libs und den Espresso-libs.
Dann fand ich ein kürzlich aktualisiertes Tutorial von Roger Hu " UI Testting with Espresso ". Ich habe eine Bemerkung bemerkt, dass Espresso Marshmallow noch nicht unterstützt.
Die Abhängigkeiten wurden wie folgt hinzugefügt:
androidTestCompile('com.Android.support.test.espresso:espresso-core:2.2') {
// Necessary if your app targets Marshmallow (since Espresso
// hasn't moved to Marshmallow yet)
exclude group: 'com.Android.support', module: 'support-annotations'
}
androidTestCompile('com.Android.support.test:runner:0.3') {
// Necessary if your app targets Marshmallow (since the test runner
// hasn't moved to Marshmallow yet)
exclude group: 'com.Android.support', module: 'support-annotations'
}
Dies löste meinen Abhängigkeitskonflikt und ich konnte keine weiteren Probleme feststellen.
Ich habe das Problem gelöst, indem Sie Folgendes manuell importiert haben. Ich dachte, es sollte automatisch importiert werden, aber es gelang nicht:
import static Android.support.test.espresso.Espresso.onView;
import static Android.support.test.espresso.action.ViewActions.click;
import static Android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
import static Android.support.test.espresso.action.ViewActions.typeText;
import static Android.support.test.espresso.assertion.ViewAssertions.matches;
import static Android.support.test.espresso.matcher.ViewMatchers.withId;
import static Android.support.test.espresso.matcher.ViewMatchers.withText;
Wie oben beschrieben, sind folgende Änderungen gegeben:
androidTestCompile 'com.Android.support.test:runner:0.3'
sie müssen zu wechseln
androidTestCompile('com.Android.support.test:runner:0.3') {
exclude group: 'com.Android.support', module: 'support-annotations'
}
und für mich funktionierte es nicht einmal mit dem oben genannten Wechsel, was mir auffiel, war, dass mir die folgende Aufnahme fehlte:
androidTestCompile 'com.Android.support.test.uiautomator:uiautomator-v18:2.1.1'
und es hat gut für mich funktioniert.
Das komplette build.gradle kann wie folgt gefunden werden:
apply plugin: 'com.Android.application'
Android {
compileSdkVersion 23
buildToolsVersion "21.1.2"
lintOptions {
// IMPORTANT: We are disabling this rule to avoid build errors on PrettyTime. Although
//pretty time references an InvalidPackage it does not do it in the code sections we use
//given how easy this library is to use I would prefer not to replace it with something
//like Joda-Time which is overkill for such a small section of the app.
disable 'InvalidPackage'
}
packagingOptions {
exclude 'LICENSE.txt'
}
defaultConfig {
applicationId "co.test.dialer"
minSdkVersion 18
targetSdkVersion 22
versionCode 15
versionName "0.6.15."
renderscriptTargetApi 22
renderscriptSupportModeEnabled true
testInstrumentationRunner "Android.support.test.runner.AndroidJUnitRunner"
}
signingConfigs {
production {
storeFile file("keystore.jks")
storePassword "hello"
keyAlias "production"
keyPassword "Android"
}
debug {
storeFile file("keystore.jks")
storePassword "hello"
keyAlias "debug"
keyPassword "Android"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-Android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.production
}
debug {
minifyEnabled false
debuggable true
applicationIdSuffix ".debug"
signingConfig signingConfigs.debug
}
internal_test {
minifyEnabled false
debuggable true
applicationIdSuffix ".test"
signingConfig signingConfigs.debug
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.Android.support:appcompat-v7:23.0.1'
compile 'com.Android.support:support-v13:23.0.1'
compile 'com.Android.support:cardview-v7:23.0.1'
compile 'com.Android.support:design:23.0.1'
compile 'com.Android.support:recyclerview-v7:23.0.1'
compile 'com.google.Android.gms:play-services-gcm:8.1.0'
compile 'com.jakewharton:butterknife:6.1.0'
compile 'com.afollestad:material-dialogs:0.7.8.0'
compile 'com.googlecode.libphonenumber:libphonenumber:3.1'
compile 'com.mcxiaoke.volley:library:1.0.15'
compile 'squizbit.com.jsonobjectified:jetjson:[email protected]'
compile 'com.google.Android.gms:play-services-analytics:8.1.0'
releaseCompile 'co.test.dialersdk:dialersdk:[email protected]';
debugCompile 'co.test.dialersdk:dialersdk-debug:[email protected]';
internal_testCompile 'co.test.dialersdk:dialersdk-internal_test:[email protected]';
androidTestCompile('com.Android.support.test:runner:0.3') {
exclude group: 'com.Android.support', module: 'support-annotations'
}
androidTestCompile('com.Android.support.test:rules:0.3') {
exclude group: 'com.Android.support', module: 'support-annotations'
}
androidTestCompile('com.Android.support.test.espresso:espresso-core:2.2') {
exclude group: 'com.Android.support', module: 'support-annotations'
}
androidTestCompile('com.Android.support.test.espresso:espresso-intents:2.2') {
exclude group: 'com.Android.support', module: 'support-annotations'
}
androidTestCompile('com.Android.support.test.espresso:espresso-contrib:2.2') {
exclude group: 'com.Android.support', module: 'support-annotations'
exclude group: 'com.Android.support', module: 'appcompat'
exclude group: 'com.Android.support', module: 'support-v4'
exclude module: 'recyclerview-v7'
}
androidTestCompile('com.Android.support.test.espresso:espresso-web:2.2') {
exclude group: 'com.Android.support', module: 'support-annotations'
}
androidTestCompile 'com.Android.support.test.uiautomator:uiautomator-v18:2.1.1'
}
Ich hoffe, dass dies sicherlich jemandem helfen wird, da ich mich einen halben Tag lang darum gekümmert habe, das Problem zu beheben, auch wenn Sie die kompletten Schritte des Vogella-Tutorials befolgt haben.
Ich habe es mit change the constant gelöst
minSdkVersion
auf Version 18 in der Datei build.gradle .
Folgende gradle.file arbeitet:
apply plugin: 'com.Android.application'
Android {
compileSdkVersion 23
buildToolsVersion "23.0.0"
defaultConfig {
applicationId "com.something.xxx"
minSdkVersion 18
targetSdkVersion 23
versionCode 1
versionName "1.0"
testInstrumentationRunner "Android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-Android.txt'), 'proguard-rules.pro'
}
}
lintOptions {
abortOnError false
}
packagingOptions {
exclude 'LICENSE.txt'
}
}
repositories {
mavenCentral()
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
maven { url "https://jitpack.io" }
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.Android.support:appcompat-v7:23.0.0'
compile 'com.google.Android.gms:play-services:7.8.0'
compile 'com.mcxiaoke.volley:library:1.0.18'
compile 'com.orhanobut:logger:1.11'
// TESTING DEPENDENCIES
androidTestCompile 'com.Android.support:support-annotations:23.0.0'
androidTestCompile 'com.Android.support.test:runner:0.3'
// Set this dependency to use JUnit 4 rules
androidTestCompile 'com.Android.support.test:rules:0.3'
// Set this dependency to build and run Espresso tests
androidTestCompile 'com.Android.support.test.espresso:espresso-core:2.2'
// add this for intent mocking support
androidTestCompile 'com.Android.support.test.espresso:espresso-intents:2.2'
// add this for webview testing support
androidTestCompile 'com.Android.support.test.espresso:espresso-web:2.2'
// Set this dependency to build and run UI Automator tests
androidTestCompile 'com.Android.support.test.uiautomator:uiautomator-v18:2.1.1'
androidTestCompile 'com.Android.support.test.espresso:espresso-contrib:2.2'
}
Möglicherweise wird diese Fehlermeldung angezeigt, weil der Ordner, in dem sich der Test befindet, nicht der Spezifikation entspricht. Der Ordner muss src/androidTest/Java sein.
Schau dir diesen Artikel an, der sagt ...
Instrumentierte Gerätetests ausführen Um Ihre instrumentierten Tests auszuführen, folgen Sie diese Stufen:
Stellen Sie sicher, dass Ihr Projekt mit Gradle synchronisiert ist, indem Sie auf Sync .__ klicken. Projekt in der Symbolleiste. Führen Sie Ihren Test auf eine der folgenden Arten aus: Um einen einzelnen Test auszuführen, öffnen Sie das Projektfenster und klicken Sie mit der rechten Maustaste auf eine testen Sie und klicken Sie auf Ausführen. Um alle Methoden in einer Klasse zu testen, klicken Sie mit der rechten Maustaste auf eine Klasse oder Methode in der Testdatei und klicken Sie auf Ausführen. Alle Tests in einem .__ ausführen. Klicken Sie mit der rechten Maustaste auf das Verzeichnis, und wählen Sie Tests ausführen. Das Das Android Plugin für Gradle kompiliert den instrumentierten Testcode Erstellt im Standardverzeichnis (src/androidTest/Java /) einen Test-APK und Produktions-APK installiert beide APKs auf dem angeschlossenen Gerät oder Emulator und führt die Tests aus. Android Studio zeigt dann die Ergebnisse an der instrumentierten Testausführung im Ausführungsfenster.
Also Leute, für Instrumentationstest muss der Ordner sein (Fall nicht vergessen)
src/androidTest/Java
und für local-Tests muss der Ordner sein.
src/test/Java
Sie können dann Ihre Paketordner so einrichten, dass sie zu Ihrem App-Paket passen
Hoffe, das hilft der Community!
Ich hatte das gleiche Problem und habe das Ändern meiner Build-Variante behoben. Ich habe den Test im Release-Build ausgeführt.
Möglicherweise haben Sie mehrere Build-Typen. Default Android Project erstellt zwei Build-Typen (Debug/Release)
http://tools.Android.com/tech-docs/new-build-system/user-guide#TOC-Testing
Currently only one Build Type is tested. By default it is the debug Build Type, but this can be reconfigured with:
Android {
...
testBuildType "staging"
}
Sie können auf diese Antwort verweisen.
"Ich habe den Fehler gemacht, die Testklassen auf src/test zu setzen. Nachdem sie auf src/androidTest/Java/verschoben wurden, wurde die Abhängigkeit gelöst. Vielleicht ist das auch Ihr Problem."