Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InOrder.verify with non-mock should perhaps give standard non-mock warning #2242

Closed
mjustin opened this issue Mar 25, 2021 · 1 comment
Closed

Comments

@mjustin
Copy link

mjustin commented Mar 25, 2021

Minor, but I noticed today when I accidentally passed in a non-mock to an InOrder.verify call that it warned me that it can only verify mocks that were passed in during creation of InOrder. While correct, it seems like perhaps using the standard "non-mock" warning would be more helpful as it has better hinting for this situation. In particular, this seems like it would be useful to newer users of the API who might have a hard time figuring out what they did wrong.

Mockito.verify

List<?> mock = Mockito.mock(List.class);
Mockito.verify(Mockito.mock.get(5));

Output

Argument passed to verify() should be a mock but is null!
Examples of correct verifications:
    verify(mock).someMethod();
    verify(mock, times(10)).someMethod();
    verify(mock, atLeastOnce()).someMethod();
    not: verify(mock.someMethod());
Also, if you use @Mock annotation don't miss initMocks()

inOrder.verify

List<?> mock = Mockito.mock(List.class);
InOrder inOrder = Mockito.inOrder(mock);
inOrder.verify(mock.get(5));

Output

InOrder can only verify mocks that were passed in during creation of InOrder.
For example:
    InOrder inOrder = inOrder(mockOne);
    inOrder.verify(mockOne).doStuff();

Desired behavior

Note that the current InOrder message does not give the hint of how to fix the non-mock that was passed in. Using the standard non-mock warning in this case seems like a more helpful response. Alternatively, that the hinting could be included in the InOrder output when a non-mock is used.

@TimvdLippe
Copy link
Contributor

PRs welcome 😄

devintark added a commit to devintark/mockito that referenced this issue Apr 9, 2021
… / specific

*Adds NullInsteadOfMockException and NotAMockException checks to Inorder.verify() *before* checking if mocks were passed in during creation of Inorder
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants