如何在Android中对对onServiceConnected的调用进行单元测试

问题描述

晚上好,我的课堂上有这段代码:

private val myServiceConnection: ServiceConnection = object : ServiceConnection {
    override fun onServiceConnected(className: ComponentName,service: IBinder) {
        // We've bound to LocalService,cast the IBinder and get LocalService instance
        val binder = service as AlexaService.AlexaBinder
        myService = binder.service
        isBounded = true
    }

    override fun onServiceDisconnected(arg0: ComponentName) {
        isBounded = false
    }
}

fun bindToMyService(context: Context) = with(context) {
    // Bind to MyService
    val intent = Intent(this,MyService::class.java)
    bindService(intent,myServiceConnection,Service.BIND_AUTO_CREATE)
}

所以我想测试何时调用bindToMyService,我进入onServiceConnected内部。 到目前为止,这是我的测试课程:

@Mock
private lateinit var myService: MyService

@Mock
private lateinit var context: Context

@InjectMocks
private lateinit var myClass: MyClass

@Before
fun setUp() {
    MockitoAnnotations.initMocks(this)
}

@Test
fun testConnection() {
    myClass.bindToMyService(context)
}

触发ServiceConnected时我​​需要做什么? 预先感谢您的帮助

问候

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...