He estado tratando de realizar una prueba sencilla interfaz de usuario mediante Espresso y todas mis pruebas fallan con la misma salvedad:
java.lang.IllegalStateException: No se instrumentación registrado! Debe ejecutarse bajo una instrumentación de registro
Es una guía para principiantes en el uso de esspresso aquí . Ya he encontrado preguntas similares, pero los más relevantes para mí son contestadas aquí - supongo que es porque no se pintan el cuadro completo así que aquí está mi código. Sólo voy a mostrar una prueba porque todos fallan con el mismo error exacto:
build.gradle (Módulo: app)
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'
}
build.gradle (Proyecto: 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
}
corrección de aquí son las dos pruebas. No creo que el error es de ninguna manera relacionada con las mismas pruebas, pero las configuraciones - mi error aunque
(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)));
}
}
He tratado siguiendo esta documentación espresso establecido aquí pero seguir recibiendo el mismo error:
pruebas iniciadas en ejecución
java.lang.IllegalStateException: No se instrumentación registrado! Debe ejecutarse bajo una instrumentación de registrarse. en androidx.test.InstrumentationRegistry.getInstrumentation (InstrumentationRegistry.java:50) en androidx.test.InstrumentationRegistry.getTargetContext (InstrumentationRegistry.java:101) en androidx.test.rule.ActivityTestRule. (ActivityTestRule.java:144) en androidx.test .rule.ActivityTestRule. (ActivityTestRule.java:120) en androidx.test.rule.ActivityTestRule. (ActivityTestRule.java:103) en io.github.vinge1718.myrestaurants.MainActivityInstrumentationTest. (MainActivityInstrumentationTest.java:25) en java.lang .reflect.Constructor.newInstance0 (Método nativo) en java.lang.reflect.Constructor.newInstance (Constructor.java:334) en org.junit.runners.BlockJUnit4ClassRunner.createTest (BlockJUnit4ClassRunner.java:217) en org.junit.runners .
Las pruebas corrieron hasta su finalización.
Aquí está mi configuraciones de prueba según lo descrito en el espresso configurar la documentación :
Creo que esto se debe a que las bibliotecas androidx
está en conflicto con com.android.support.test
. Si desea utilizar jetpack, debe convertir todos biblioteca de prueba a androidx, si usted no quiere de esa manera, simplemente borrar sus androidx
bibliotecas y utilizar todos com.android.support.test
. Comprobar mi última respuesta en Android Prueba Instrumentación: No se instrumentación de error registrado . Espero que esto te ayude.