我试图在新的Android项目中创建Espresso UI测试,但我遇到了以下问题。
如果我试图创建一个空的测试类:
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> {
}
我总是收到这样的错误消息:
cannot resolve symbol AndroidJUnit4.class
并且几乎所有导入的库都被标记为未使用。
build.gradle文件包含以下内容:
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'
}
如果我把这些设置放在我的其他测试项目上,它就可以工作了,所以我不知道哪里会出错?
我遵循了这个教程:“
http://www.vogella.com/tutorials/AndroidTestingEspresso/article.html
我试着通过下面的问题来解决这个问题:Cannot resolve symbol 'AndroidJUnit4'
但是没有那么幸运。
非常感谢您的建议。
发布于 2015-09-07 21:26:46
我也尝试过vogella的相同教程,遇到了很多问题。我遇到的第一个问题是v23库和Espresso库的注释版本之间的依赖冲突。
然后我找到了罗杰·胡最近更新的另一个教程"UI Testting with Espresso“。我注意到一个评论说Espresso还不支持棉花糖。
添加依赖项的方式如下:
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'
}
这解决了我的依赖冲突,我没有看到任何其他的问题发生。
发布于 2016-02-11 17:11:43
我通过手动导入解决了它,我认为它应该是自动导入的,但它没有:
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;
发布于 2015-11-09 11:29:16
根据上面给出的gradle更改:
androidTestCompile 'com.android.support.test:runner:0.3'
您需要更改为
androidTestCompile('com.android.support.test:runner:0.3') {
exclude group: 'com.android.support', module: 'support-annotations'
}
对我来说,即使在上面的更改中它也不起作用,所以我注意到我遗漏了下面的内容:
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'
对我来说效果很好。
完整的build.gradle如下所示:
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:1.0.3@aar'
compile 'com.google.android.gms:play-services-analytics:8.1.0'
releaseCompile 'co.test.dialersdk:dialersdk:1.0.8@aar';
debugCompile 'co.test.dialersdk:dialersdk-debug:1.0.8@aar';
internal_testCompile 'co.test.dialersdk:dialersdk-internal_test:1.0.8@aar';
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'
}
我希望这一定会帮助一些人,因为我已经挣扎了半天来修复它,即使在遵循了vogella教程的完整步骤之后也是如此。
https://stackoverflow.com/questions/32175847
复制相似问题