Vic*_*nge 6 java junit android unit-testing android-espresso
我一直在尝试使用Espresso执行简单的UI测试,但我的所有测试均因相同的异常而失败:
java.lang.IllegalStateException:未注册任何工具!必须在注册工具下运行
这是在这里使用浓缩咖啡的初学者指南。我已经找到了类似的问题,但是与我最相关的问题在这里没有得到解答-我想这是因为它们没有画出全部图片,所以这是我的代码。我将只显示一个测试,因为它们全部失败,并产生完全相同的错误:
build.gradle(模块:应用)
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
applicationId "io.github.vinge1718.myrestaurants"
minSdkVersion 15
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
//testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
testOptions {
unitTests {
includeAndroidResources = true
}
}
}
dependencies {
testImplementation "org.robolectric:robolectric:3.8"
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation('com.android.support.test.espresso:espresso-core:3.0.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
androidTestImplementation 'androidx.test:runner:1.1.0'
androidTestImplementation 'androidx.test:rules:1.1.0'
androidTestImplementation 'org.hamcrest:hamcrest-library:1.3'
}
Run Code Online (Sandbox Code Playgroud)
build.gradle(项目:MyRestaurant)
buildscript {
repositories {
google()
jcenter()
maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Run Code Online (Sandbox Code Playgroud)
校正这里是两个测试。我不认为该错误与测试本身有任何关系,但与配置有关-尽管我已经纠正了
(MainActivityInstrumentationTest.java)
package io.github.vinge1718.myrestaurants;
import android.support.test.runner.AndroidJUnit4;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import androidx.test.rule.ActivityTestRule;
import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.action.ViewActions.closeSoftKeyboard;
import static androidx.test.espresso.action.ViewActions.typeText;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
@RunWith(AndroidJUnit4.class)
public class MainActivityInstrumentationTest {
private String mStringToBetyped;
@Rule
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(MainActivity.class);
@Before
public void initValidString() {
// Specify a valid string.
mStringToBetyped = "Portland";
}
@Test
public void validateEditText(){
onView(withId(R.id.locationEditText))
.perform(typeText(mStringToBetyped), closeSoftKeyboard())
.check(matches(withText(mStringToBetyped)));
}
@Test
public void locationIsSentToRestaurantActivity(){
String location = "Portland";
onView(withId(R.id.locationEditText)).perform(typeText(location));
onView(withId(R.id.findRestaurantsButton)).perform(click());
onView(withId(R.id.locationTextView)).check(matches(withText("Here are all the Restaurants near " + location)));
}
}
Run Code Online (Sandbox Code Playgroud)
我在这里尝试按照此espresso设置文档进行操作,但仍然出现相同的错误:
开始运行测试
java.lang.IllegalStateException:未注册任何工具!必须在注册工具下运行。在androidx.test.InstrumentationRegistry.getInstrumentation(InstrumentationRegistry.java:50)在androidx.test.InstrumentationRegistry.getTargetContext(InstrumentationRegistry.java:101)在androidx.test.rule.ActivityTestRule。(ActivityTestRule.java:144)在androidx。 .rule.ActivityTestRule。(ActivityTestRule.java:120)位于androidx.test.rule.ActivityTestRule。(ActivityTestRule.java:103)位于io.github.vinge1718.myrestaurants.MainActivityInstrumentationTest。(MainActivityInstrumentationTest.java:25) org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java上的java.lang.reflect.Constructor.newInstance(Constructor.java:334)上的.reflect.Constructor.newInstance0(本机方法):
测试完成。
这是espresso设置文档中描述的我的测试配置:
我认为这是因为库androidx与com.android.support.test. 如果你想使用jetpack,你必须将所有测试库转换为androidx,如果你不想这样,只需删除你的androidx库并使用所有com.android.support.test. 检查我在Android Instrumentation Test: No Instrumentation Registration Error中的最新答案。希望这对您有帮助。
| 归档时间: |
|
| 查看次数: |
2520 次 |
| 最近记录: |