无法验证 Mockito 中的俘虏
Can't verify captor in Mockito
写了下一个测试:
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:
Argument passed to verify() is of type Unit and is not a mock!
Make sure you place the parenthesis correctly!
See the examples of correct verifications:
verify(mock).someMethod();
verify(mock, times(10)).someMethod();
verify(mock, atLeastOnce()).someMethod();
你是怎么定义的
notificationManager
使用 MockitoAnnotations,您可以在测试中定义一个字段,如下所示
@Mock
NotificationManager notificationManager
@Before
public void setup()
{
MockitoAnnotations.init(this);
}
注意: 监视 class 它必须是模拟对象或已被监视的 真实 对象在。即
val notificationManagerSpy: spy(notificationManager)
如果您使用的是 Robolectric you will have a real ShadowNotificationManager 等测试框架,那么您应该监视真实对象。解决方案如下:
@RequiresApi(Build.VERSION_CODES.O)
@Test
@Throws(Exception::class)
fun clearNotificationsTest()
{
val notificationManagerSpy: spy(notificationManager)
Mockito.`when`(mockContext.getSystemService(Context.NOTIFICATION_SERVICE))
.thenReturn(notificationManagerSpy)
val captor: ArgumentCaptor<NotificationChannel> =
ArgumentCaptor.forClass(NotificationChannel::class.java)
mockContext.registerNotificationChannels()
Mockito.verify(notificationManagerSpy
.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))
请参阅:https://www.baeldung.com/mockito-spy 了解更多关于间谍以及什么是有效的可间谍对象的信息。
这就是你使用 mock 编写验证的方式
Mockito.verify(notificationManager).createNotificationChannel(captor.capture()));
写了下一个测试:
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: Argument passed to verify() is of type Unit and is not a mock! Make sure you place the parenthesis correctly! See the examples of correct verifications: verify(mock).someMethod(); verify(mock, times(10)).someMethod(); verify(mock, atLeastOnce()).someMethod();
你是怎么定义的
notificationManager
使用 MockitoAnnotations,您可以在测试中定义一个字段,如下所示
@Mock
NotificationManager notificationManager
@Before
public void setup()
{
MockitoAnnotations.init(this);
}
注意: 监视 class 它必须是模拟对象或已被监视的 真实 对象在。即
val notificationManagerSpy: spy(notificationManager)
如果您使用的是 Robolectric you will have a real ShadowNotificationManager 等测试框架,那么您应该监视真实对象。解决方案如下:
@RequiresApi(Build.VERSION_CODES.O)
@Test
@Throws(Exception::class)
fun clearNotificationsTest()
{
val notificationManagerSpy: spy(notificationManager)
Mockito.`when`(mockContext.getSystemService(Context.NOTIFICATION_SERVICE))
.thenReturn(notificationManagerSpy)
val captor: ArgumentCaptor<NotificationChannel> =
ArgumentCaptor.forClass(NotificationChannel::class.java)
mockContext.registerNotificationChannels()
Mockito.verify(notificationManagerSpy
.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))
请参阅:https://www.baeldung.com/mockito-spy 了解更多关于间谍以及什么是有效的可间谍对象的信息。
这就是你使用 mock 编写验证的方式
Mockito.verify(notificationManager).createNotificationChannel(captor.capture()));