AndroidJUnit4应该具有公共构造函数

时间:2018-11-21 17:21:53

标签: android android-testing android-instrumentation

我正在尝试遵循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设备一样,该解决方案要么已过时,要么就是根本不起作用。不幸的是,在所有解决方案中,我什至无法认出一个共同的主题,从而使我陷入困境。

我该如何解决该错误并仅使仪器化单元测试运行?

1 个答案:

答案 0 :(得分:0)

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

相关问题