How can I verify a method is run in a mocked class's callback?

廉价感情. 提交于 2019-12-06 08:22:31

Use Mockito.when to make the createUserCall return a mocked Task. Then Mockito.verify on the task to capture the arguments to the add listener call.

Test the captured arguments to the extent you heart desires (this is like a unit test within a unit test, the captured arguments are your new Classes under test).

This method won't actually test that the listeners are called. Just that the add listener method was called and that the callbacks do what they should when called

verify(mockTask).addOnSuccessListener(listenerCaptor.capture());
OnSuccessListener<Auth> newObjectUnderTest = listenerCaptor.getValue();

//ACT
newObjectUnderTest.onSuccess(auth);

//ASSERT
verify(authListener).newUserCreated();

Use Mockito.when to make the createUserCall return an already completed Task<AuthResult>.

Then Mockito.verify that the authListener did what it should assuming authListener is also a mock

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!