[英]Android local unit test issue[JUnit 4.12]
我正在编写非常简单的本地单元测试来获取应用程序上下文并使用它读取字符串。 我们的目的是对我们编写的 API 客户端和接口进行单元测试,以使用 retrofit 2x 进行 API 调用。
在 build.gradle 文件中添加了以下依赖项
构建.gradle:
defaultConfig {
applicationId "x"
minSdkVersion 23
targetSdkVersion 28
versionCode 89
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
androidTestImplementation 'androidx.test:rules:1.3.0-alpha02'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.annotation:annotation:1.1.0'
testImplementation 'com.squareup.okhttp3:mockwebserver:3.2.0'
testImplementation 'androidx.test:core:1.2.0'
本地单元测试类:
import android.content.Context
import androidx.test.core.app.ApplicationProvider
import org.junit.Test
class ExampleAPITest {
@Test
fun useAppContext() {
// val context: Context = InstrumentationRegistry.getInstrumentation().targetContext
val context: Context = ApplicationProvider.getApplicationContext<Context>()
val appName = context.getString(R.string.app_name)
println(appName)
}
}
我们正在使用 Espresso 进行测试自动化。 所以我们在 gradle 文件中添加了两个依赖项。 我们在运行本地单元测试 ExampleUnitTest 类时遇到以下异常,
java.lang.IllegalStateException: No instrumentation registered! Must run under a registering instrumentation.
at androidx.test.platform.app.InstrumentationRegistry.getInstrumentation(InstrumentationRegistry.java:45)
at androidx.test.core.app.ApplicationProvider.getApplicationContext(ApplicationProvider.java:41)
at com.trimble.cec.appname.ExampleAPITest.loginApiTest(ExampleAPITest.kt:19)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
我知道我们可以使用 mockito 或 Robolectric 来模拟/访问 android 框架,我们只需要上下文来调用 api 服务。 无论如何,我们将自动化 API 调用作为 Android 测试的一部分。 我们需要构建本地测试以在本地测试 API 调用。
我们使用 Java 编写了 android 测试,使用 Kotlin 编写了本地单元测试。
浏览上述异常并尝试不同的依赖版本,但没有任何帮助。 看起来这是一个 android 支持测试核心库问题。 有人可以帮我解决这个问题吗? 谢谢!
@RunWith
注释似乎丢失了:
import org.junit.runner.RunWith;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import junit.framework.TestCase;
...
@RunWith(AndroidJUnit4.class)
class ExampleAPITest extends TestCase {
...
}
最好使用与其他依赖项的版本号一致的稳定版本:
androidTestImplementation "androidx.test:rules:1.2.0"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.