【发布时间】:2019-06-27 12:24:13
【问题描述】:
写下一个测试:
private val mockContext = Mockito.mock(Context::class.java)
private val notificationManager = Mockito.mock(NotificationManager::class.java)
@RequiresApi(Build.VERSION_CODES.O)
@Test
@Throws(Exception::class)
fun clearNotificationsTest() {
Mockito.`when`(mockContext.getSystemService(Context.NOTIFICATION_SERVICE)).thenReturn(notificationManager)
val captor: ArgumentCaptor<NotificationChannel> = ArgumentCaptor.forClass(NotificationChannel::class.java)
mockContext.registerNotificationChannels()
Mockito.verify(notificationManager.createNotificationChannel(captor.capture()))
val argument: NotificationChannel = captor.value
assertThat(argument.id, equalTo(CHANNEL_ID))
assertThat(argument.name.toString(), equalTo(CHANNEL_NAME))
assertThat(argument.importance, equalTo(NotificationManagerCompat.IMPORTANCE_HIGH))
}
在我的verify 中得到下一个错误:
org.mockito.exceptions.misusing.NotAMockException: 传递给 verify() 的参数是 Unit 类型,不是模拟! 确保正确放置括号! 查看正确验证的示例: 验证(模拟).someMethod(); 验证(模拟,次(10))。someMethod(); 验证(模拟,atLeastOnce()).someMethod();
【问题讨论】:
-
该测试应该做什么?对我来说,它就像你只调用模拟(mockContext和notificationManager)上的方法一样锁定,所以你没有在这里测试任何东西......
-
并且验证的语法应该是 Mockito.verify(notificationManager).createNotificationChannel(captor.capture()),假设您想验证与 createNotificationChannel 方法的一些交互。但很可能没有,因为调用
mockContext.registerNotificationChannels()什么都不做。