如何解决如何在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 举报,一经查实,本站将立刻删除。