Android Espresso:无法解析符号AndroidJUnit4.class [英] Android Espresso : cannot resolve symbol AndroidJUnit4.class
问题描述
我正在尝试在新的Android项目中创建Espresso UI测试,但遇到了以下问题.
I'm trying to create Espresso UI test inside the new Android project but I faced with the following problem.
如果我尝试创建一个空的测试类:
If I tried to create a empty test class:
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> {
}
我总是收到此错误消息:
I always get this error message:
cannot resolve symbol AndroidJUnit4.class
几乎所有导入的库都标记为未使用.
And almost all imported libraries are marked as unused.
build.gradle文件包含以下内容:
build.gradle file is containing the following:
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'
}
如果将这些设置放在其他测试项目中,它将起作用,所以我不知道有什么问题吗?
If I put these setting on my other test project it works, so I don't know what can be wrong?
我已经遵循了本教程:"
I've followed this tutorial:"
http://www.vogella.com/tutorials/AndroidTestingEspresso/article.html
我试图通过以下SO问题来解决它: 无法解析符号'AndroidJUnit4'
And I've tried to resolve it by following SO question: Cannot resolve symbol 'AndroidJUnit4'
但是没有运气.
非常感谢您的任何建议.
Many thanks for any advice.
推荐答案
我也尝试过vogella的同一教程,遇到了很多问题.我遇到的第一个问题是v23库的注释版本和Espresso库的注释版本之间的依赖冲突.
I've tried the same tutorial from vogella too and ran into many issues. One of the first issues I ran into was a dependency clash between the annotation versions of v23 libs and the Espresso libs.
然后我从Roger Hu找到了另一个最近更新的教程"使用Espresso进行UI测试 ".我注意到有句话说,意式浓缩咖啡还不支持棉花糖.
Then I found another recently updated tutorial from Roger Hu "UI Testting with Espresso". I noticed a remark that Espresso is not supporting Marshmallow yet.
依赖项添加如下:
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'
}
这解决了我的依赖冲突,并且我没有看到其余的任何问题.
This solved my dependency conflict and I didn't see any of the rest of the issues occurring.
这篇关于Android Espresso:无法解析符号AndroidJUnit4.class的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!