AndroidJUnit4应该有一个公共构造函数



我正试图按照Android开发人员文档编写一个工具化单元测试,但当然,它不起作用。我得到错误:

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)
}
}

我已经尝试了多种解决方案来解决我在网上发现的这个特定错误,但就像大多数安卓系统一样,这个解决方案要么过时了,要么根本不起作用。不幸的是,在所有的解决方案中,我甚至无法识别一个共同的主题,从而使我陷入困境。

我如何解决错误并运行一个Instrumented Unit Test?

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包冲突。

相关内容

最新更新