Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何为定制广播接收机编写mockito测试_Android_Unit Testing_Mockito_Broadcastreceiver - Fatal编程技术网

Android 如何为定制广播接收机编写mockito测试

Android 如何为定制广播接收机编写mockito测试,android,unit-testing,mockito,broadcastreceiver,Android,Unit Testing,Mockito,Broadcastreceiver,我有一个自定义的广播接收器类,我在应用程序的多个地方使用,它工作良好。现在我想用mockito编写单元测试,但我无法实现。此自定义广播正在与绑定服务通信 我跟踪了一些SO问题和谷歌文章,没有任何帮助 下面的文章我一直在关注 我试过booth,但没用 下面的代码将说明我的自定义广播接收器是如何工作的 class CustomBroadcastReceiver(val onResultListener: OnResultListener) : BroadcastReceiver() { ove

我有一个自定义的广播接收器类,我在应用程序的多个地方使用,它工作良好。现在我想用mockito编写单元测试,但我无法实现。此自定义广播正在与绑定服务通信

我跟踪了一些SO问题和谷歌文章,没有任何帮助

下面的文章我一直在关注

我试过booth,但没用

下面的代码将说明我的自定义广播接收器是如何工作的

class CustomBroadcastReceiver(val onResultListener: OnResultListener) :
BroadcastReceiver() {

override fun onReceive(context: Context?, intent: Intent?) {
    when (intent!!.action) {
        Constants.STATUS_ONE -> {
           onResultListener.connectionSuccess()
        }
        Constants.DATA_AVAILABLE -> {                
            onResultListener.onDataAvailable(data)
        }
        Constants.GATT_READ -> {
            onResultListener.onRead(readData)
        }}



interface OnResultListener {

    fun connectionSuccess()

    fun connectionFail(isFromConnection: Boolean, isFrom: String = Constants.DEFAULT)

    fun onDataAvailable(data: ArrayList<Data>)

    fun onCharacteristicRead(data:Data)}}

为什么需要测试Androids广播接收器?我们可以假设Android已经测试过它,并且它可以工作。@JakeB在这个定制的广播接收器中,我有多个动作,有很多工作正在进行,我想用Mockito编写单元测试用例,你应该测试实现该接口的类。你在这里测试的基本上是一个Switch语句…@JakeB要测试这个接口,我也无法做到,接口将从接收器触发,我无法模拟它。你发布的代码不需要测试。实现类执行(无论它们是什么)。
testImplementation 'org.mockito:mockito-core:2.24.5'
androidTestImplementation 'org.mockito:mockito-core:2.24.5'
testImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0"