java Espresso UI 测试无法识别 onView()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34927723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Espresso UI testing doesn't recognize onView()
提问by somerandomusername
I have spent all day setting up Junit4 instrumentation tests with Espresso, but just can't seem to get that final step. No matter what I do it won't recognize the onView()
method. I have tried multiple SDK/support-lib versions and so far nothing. I have gone through all the google setup guides and many stackoverflow posts, turning here as my last hope.
我花了一整天的时间用 Espresso 设置 Junit4 仪器测试,但似乎无法完成最后一步。无论我做什么,它都无法识别该onView()
方法。我已经尝试了多个 SDK/support-lib 版本,但到目前为止一无所获。我已经浏览了所有 google 设置指南和许多 stackoverflow 帖子,将这里作为我最后的希望。
Gradle version : com.android.tools.build:gradle:1.5.0
摇篮版本: com.android.tools.build:gradle:1.5.0
Gradle file:
摇篮文件:
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
useLibrary 'org.apache.http.legacy'
defaultConfig {
applicationId "lv.my.android"
minSdkVersion 9
targetSdkVersion 22
testApplicationId "lv.my.android.tests"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
packagingOptions {
exclude 'LICENSE.txt'
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/LICENSE'
exclude 'META-INF/NOTICE'
}
compileOptions {
sourceCompatibility = 'VERSION_1_7'
targetCompatibility = 'VERSION_1_7'
}
signingConfigs {
beta {
storeFile file("beta.keystore")
storePassword "betabuild"
keyAlias "key"
keyPassword "betabuild"
}
release
}
buildTypes {
debug {
debuggable true
applicationIdSuffix '.debug'
versionNameSuffix '-DEV'
minifyEnabled false
}
beta {
debuggable true
applicationIdSuffix '.beta'
versionNameSuffix '-BETA'
signingConfig signingConfigs.beta
minifyEnabled false
}
release {
minifyEnabled false
}
}
}
dependencies {
compile files('src/main/libs/guice-3.0-no_aop.jar')
compile files('src/main/libs/javax.inject-1.jar')
compile files('src/main/libs/roboguice-2.0.jar')
compile files('src/main/libs/junit-4.11.jar')
compile files('src/main/libs/hamcrest-core-1.3.jar')
compile files('src/main/libs/GeoLib.jar')
compile files('src/main/libs/GeoPolygons.jar')
compile files('src/main/libs/universal-image-loader-1.9.4.jar')
compile files('src/main/libs/javax.annotation-3.2-b06-sources.jar')
compile 'uk.co.chrisjenx:calligraphy:2.1.0'
compile 'com.squareup:otto:1.3.5'
compile 'com.google.android.gms:play-services:6.5.87'
compile 'com.android.support:support-annotations:23.0.1'
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:support-v4:23.0.1'
compile 'com.android.support:palette-v7:23.0.1'
compile 'com.google.code.findbugs:jsr305:2.0.1'
compile 'com.nineoldandroids:library:2.4.0'
compile 'pl.charmas.android:android-reactive-location:0.4@aar'
compile 'io.reactivex:rxjava:1.0.3'
compile files('src/main/libs/FlurryAnalytics-6.1.0.jar')
compile 'com.github.castorflex.smoothprogressbar:library:1.1.0'
androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
androidTestCompile'com.android.support.test:runner:0.4.1'
// Set this dependency to use JUnit 4 rules
androidTestCompile'com.android.support.test:rules:0.4'
// Set this dependency to build and run Espresso tests
androidTestCompile'com.android.support.test.espresso:espresso-core:2.2.1'
testCompile 'junit:junit:4.12'
testCompile "org.mockito:mockito-core:1.9.5"
}
My test (located in src/androidTest/java/lv/my/test
)
我的测试(位于src/androidTest/java/lv/my/test
)
package lv.my.test;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.LargeTest;
import lv.my.android.activities.LoginActivity;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(AndroidJUnit4.class)
@LargeTest
public class LoginActivityTest {
private String mStringToBetyped;
@Rule
public ActivityTestRule<LoginActivity> mActivityRule = new ActivityTestRule<>(LoginActivity.class);
@Before
public void initValidString() {
}
@Test
public void changeText_sameActivity() {
// onView not recognized here
}
}
PS. I use roboguice could that be the issue?
附注。我使用 roboguice 这可能是问题吗?
回答by Egor
You should either use a static import:
您应该使用静态导入:
import static android.support.test.espresso.Espresso.onView;
or
或者
import android.support.test.espresso.Espresso;
and call it in the following way
并按以下方式调用它
Espresso.onView()
回答by emen
To extend what Egor has suggested, in Android Studio, once you get red text at onView()
, simply press alt+enterwhen the red bulb popped up. Then choose static import.
为了扩展 Egor 的建议,在 Android Studio 中,一旦您在 处看到红色文本onView()
,只需在红色灯泡弹出时按alt+enter。然后选择静态导入。
回答by Pedro Gonzalez
Keep writing. Android Studio will suggest the needed Espresso imports, from the 'inside out'. For example: onView(withId()) First will detect withId(), then onView()
继续写。Android Studio 会从“由内而外”建议所需的 Espresso 导入。例如:onView(withId()) 首先会检测withId(),然后onView()
回答by OhhhThatVarun
For AndroidX use this
对于 AndroidX 使用这个
import androidx.test.espresso.Espresso.onView