android - AndroidJUnit4 应该有一个公共构造函数
问题描述
我正在尝试按照 Android Developer Docs 编写仪表化单元测试,但是,当然,它不起作用。我收到错误消息:
Custom runner class AndroidJUnit4 should have a public constructor with signature AndroidJUnit4(Class testClass)
当我运行我的示例测试时:
package com.devetry.ytp
import android.content.Context
import android.support.test.InstrumentationRegistry
import android.support.test.rule.ActivityTestRule
import android.support.test.runner.AndroidJUnit4
import androidx.test.filters.LargeTest
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import androidx.test.runner.AndroidJUnitRunner
import org.junit.Assert
import org.junit.Rule
@RunWith(AndroidJUnit4::class)
class ExampleAndroidTest {
/**
* VARIABLES
*/
/**
* LIFE CYCLE
*/
/**
* Example Android Test
*
* An example android test
*/
@Test
fun exampleAndroidTest() {
val context = InstrumentationRegistry.getTargetContext()
Assert.assertEquals("com.devetry.ytp", context.packageName)
}
}
我已经尝试了多种解决方案来解决我在网上发现的这个特定错误,但就像大多数 Android 设备一样,解决方案要么已经过时,要么根本不起作用。不幸的是,在所有解决方案中,我什至无法识别一个共同的主题,从而使我陷入困境。
如何解决错误并让仪器化单元测试运行?
解决方案
package com.devetry.ytp
import android.content.Context
import android.support.test.InstrumentationRegistry
import android.support.test.rule.ActivityTestRule
import android.support.test.runner.AndroidJUnit4
import androidx.test.filters.LargeTest
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
// AndroidJunitRunner and AndroidJUnit4 are not from the same pacakge
import androidx.test.runner.AndroidJUnitRunner
import org.junit.Assert
import org.junit.Rule
@RunWith(AndroidJUnit4::class)
class ExampleAndroidTest {
/**
* VARIABLES
*/
/**
* LIFE CYCLE
*/
...
}
您没有使用同一个包中的类,请确保它们是一致的。支持测试包与 androidx 包冲突。
推荐阅读
- sql - 当查询返回 NULL 时,SQL COALESCE 和 IS NULL 不返回空格
- date - 在 Kotlin 中初始化日期
- java - 在 Koltin/Java 中,如何强制使用来自 Maven 依赖项的同名类覆盖本地项目类?
- php - 如何从同一服务器上的另一个域调用 php 文件
- java - 如果 value 是实例,则 TreeMap 按值排序
- c# - SquareUp 请求令牌:禁止
- java - 如何将 JSON 转换为 POJO 类?
- xamarin.ios - 如何将文件从临时位置复制到应用程序容器 - Xamarin iOS
- c - 将指向结构数组的指针设置为等于结构数组
- ruby - 是否有 Yard 的默认 Rake 任务