Java Android本地单元测试问题[JUnit4.12]
我正在编写非常简单的本地单元测试来获取应用程序上下文并使用它读取字符串。我们的目的是对API客户机和接口进行单元测试,我们已经编写了该接口,以便使用Reformation 2x进行API调用 在build.gradle文件中添加了以下依赖项 Build.gradle:Java Android本地单元测试问题[JUnit4.12],java,android,unit-testing,kotlin,automated-tests,Java,Android,Unit Testing,Kotlin,Automated Tests,我正在编写非常简单的本地单元测试来获取应用程序上下文并使用它读取字符串。我们的目的是对API客户机和接口进行单元测试,我们已经编写了该接口,以便使用Reformation 2x进行API调用 在build.gradle文件中添加了以下依赖项 Build.gradle: defaultConfig { applicationId "x" minSdkVersion 23 targetSdkVersion 28 versionCode
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)
}
}
我知道我们可以使用mockito或Robolectric来模拟/访问android框架,我们只需要上下文来调用api服务。无论如何,作为Android测试的一部分,我们将自动化API调用。我们需要构建本地测试来本地测试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"
androidTestImplementation "androidx.test:rules:1.2.0"