Para mim, as seguintes etapas funcionaram:
1. Substitua as bibliotecas androidx pela postada aqui . minha final app/build.gradle
ficou assim:
android {
...
defaultConfig {
...
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
...
}
dependencies {
...
testImplementation 'junit:junit:4.12'
// Core library
androidTestImplementation 'androidx.test:core:1.2.0'
// AndroidJUnitRunner and JUnit Rules
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test:rules:1.2.0'
// Assertions
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.ext:truth:1.2.0'
androidTestImplementation 'com.google.truth:truth:0.42'
// Espresso dependencies
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
Em seguida, substituí manualmente os módulos importados da minha ExampleInstrumentTest.java
classe pelas classes mais recentes:
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.rule.ActivityTestRule;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(AndroidJUnit4ClassRunner.class)
public class ExampleInstrumentedTest {
...
@Rule
public final ActivityTestRule<MainActivity> main = new ActivityTestRule<>(MainActivity.class, true);
@Before
public void init() {
...
}
@Test
public void listCount() {
...
}
@Test
public void useAppContext() {
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
Assert.assertEquals("in.curioustools.aad_x_testing2", appContext.getPackageName());
System.out.println("useAppContext : Test Ran");
}
}
O que estava me incomodando era o fato de a InstrumentationRegistery
classe ainda estar obsoleta. Então eu usei InstrumentationRegistry.getInstrumentation().getTargetContext();
da androidx.test.platform.app.InstrumentationRegistry
classe.