@Test public void shouldDetectUnfinishedVerification() { verify(mock); detectsAndCleansUp(new OnStub(), UnfinishedVerificationException.class); verify(mock); detectsAndCleansUp(new OnStubVoid(), UnfinishedVerificationException.class); verify(mock); detectsAndCleansUp(new OnVerify(), UnfinishedVerificationException.class); verify(mock); detectsAndCleansUp(new OnVerifyInOrder(), UnfinishedVerificationException.class); verify(mock); detectsAndCleansUp(new OnVerifyZeroInteractions(), UnfinishedVerificationException.class); verify(mock); detectsAndCleansUp(new OnVerifyNoMoreInteractions(), UnfinishedVerificationException.class); verify(mock); detectsAndCleansUp(new OnDoAnswer(), UnfinishedVerificationException.class); }
public void unfinishedVerificationException(Location location) { UnfinishedVerificationException exception = new UnfinishedVerificationException(join( "Missing method call for verify(mock) here:", location, "", "Example of correct verification:", " verify(mock).doSomething()", "", "Also, this error might show up because you verify either of: final/private/equals()/hashCode() methods.", "Those methods *cannot* be stubbed/verified.", "" )); throw exception; }
@Test public void shouldValidateExplicitly() { verify(mock); try { Mockito.validateMockitoUsage(); fail(); } catch (UnfinishedVerificationException e) {} }
@Test public void shouldFailWithUnfinishedVerification() { withFinal = mock(WithFinal.class); verify(withFinal).foo(); try { verify(withFinal).foo(); fail(); } catch (UnfinishedVerificationException e) {} }
@Test public void shouldSayUnfinishedVerificationButNotInvalidUseOfMatchers() { verify(withFinal).finalMethod(anyObject()); try { verify(withFinal); fail(); } catch (UnfinishedVerificationException e) {} }
@Test public void shouldValidateStateWhenResetting() { //invalid verify: verify(mock); try { reset(mockTwo); fail(); } catch (UnfinishedVerificationException e) {} }
@Test public void shouldShowWhereIsUnfinishedVerification() throws Exception { unfinishedVerificationHere(); try { mock(IMethods.class); fail(); } catch (UnfinishedVerificationException e) { assertContains("unfinishedVerificationHere(", e.getMessage()); } }
@Test public void shouldValidateState() throws Exception { //when InOrder inOrder = inOrder(mock); verify(mock); // mess up state //then try { inOrder.verifyNoMoreInteractions(); fail(); } catch(UnfinishedVerificationException e) {} }
@Test public void shouldCorrectStateAfterDetectingUnfinishedVerification() { mock.simpleMethod(); verify(mock); try { verify(mock).simpleMethod(); fail(); } catch (UnfinishedVerificationException e) {} verify(mock).simpleMethod(); }
@Test(expected = UnfinishedVerificationException.class) public void verifyShouldUseProxyTarget() throws Exception { this.dateService.getDate(); verify(this.dateService, times(1)).getDate(); reset(this.dateService); }