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

Misleading behavior of Mockito.verifyZeroInteractions() #989

Closed
mockitoguy opened this issue Mar 16, 2017 · 1 comment
Closed

Misleading behavior of Mockito.verifyZeroInteractions() #989

mockitoguy opened this issue Mar 16, 2017 · 1 comment

Comments

@mockitoguy
Copy link
Member

Problem

Mockito.verifyZeroInteractions() actually does not do what it advertises. The purpose of this ticket is to discuss whether to change the behavior of this method in Mockito 3.

Details

Based on user feedback at #977, from @ffissore, "verifyZeroInteractions" API can be misleading. When you read that method in test code, you expect zero interactions with specified mocks. However, "verifyZeroInteractions" is only an alias to "verifyNoMoreInteractions" and in fact, it does not guarantee that there were zero interactions. Let's use examples to illustrate the problem

Feedback needed

Please comment / vote / state your opinion about changing the behavior of "verifyZeroInteractions" so that it actually ensures that there were zero interactions with the mock (regardless if previously verified or not).

Examples

Below examples illustrate current behavior of Mockito 2.x, in case you are puzzled what this ticket is about.

Example 1 - verifyNoMoreInteractions

mock.foo();
verify(mock).foo();
verifyNoMoreInteractions(mock); //pretty clear I think

Example 2 - verifyZeroInteractions

mock.foo();
verify(mock).foo();

verifyZeroInteractions(mock);
//above passes because verifyZeroInteractions == verifyNoMoreInteractions
//and we already have verified the only method invoked on the mock

Example 3 - verifyZeroInteractions with regular stubbing

given(mock.foo()).willReturn("x");
assertEquals("x", mock.foo());
verify(mock).foo();

verifyZeroInteractions(mock);
//above passes because verifyZeroInteractions == verifyNoMoreInteractions

Example 4 - verifyZeroInteractions with new strict stubbing

given(mock.foo()).willReturn("x");
assertEquals("x", mock.foo());

verifyZeroInteractions(mock);
//above passes because with strict stubbing
//stubbed method are implicitly verified when they are "used"
//this is one of key features of strict stubbing (DRY, don't repeat yourself)
@mockitoguy mockitoguy added this to the 3.0 milestone Mar 16, 2017
TimvdLippe pushed a commit that referenced this issue May 10, 2017
…elegating to verifyNoMoreInteractions (#995)

* Fixes #989
Introducing verifyNoInteractions, that checks if the number of
invocations on given mock(s) is zero, failing otherwise

* Added missing (?) @test annotation

* Removed try catch block by using isMock
@TimvdLippe
Copy link
Contributor

This was fixed in #989

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