@Test public void shouldVerifyOneMockButFailOnOther() throws Exception { List list = mock(List.class); Map map = mock(Map.class); list.add("one"); list.add("one"); map.put("one", 1); verify(list, times(2)).add("one"); verifyNoMoreInteractions(list); try { verifyZeroInteractions(map); fail(); } catch (NoInteractionsWanted e) {} }
@Test public void shouldAllowToExcludeStubsForVerification() throws Exception { //given when(mock.simpleMethod()).thenReturn("foo"); //when String stubbed = mock.simpleMethod(); //irrelevant call because it is stubbing mock.objectArgMethod(stubbed); //then verify(mock).objectArgMethod("foo"); //verifyNoMoreInteractions fails: try { verifyNoMoreInteractions(mock); fail(); } catch (NoInteractionsWanted e) {}; //but it works when stubs are ignored: ignoreStubs(mock); verifyNoMoreInteractions(mock); }
@Test public void shouldPrintFirstUnexpectedInvocation() { mock.oneArg(true); mock.oneArg(false); mock.threeArgumentMethod(1, "2", "3"); verify(mock).oneArg(true); try { verifyNoMoreInteractions(mock); fail(); } catch (NoInteractionsWanted e) { String expectedMessage = "\n" + "No interactions wanted here:" + "\n" + "-> at"; assertContains(expectedMessage, e.getMessage()); String expectedCause = "\n" + "But found this interaction:" + "\n" + "-> at"; assertContains(expectedCause, e.getMessage()); } }
@Test public void shouldPrintFirstUnexpectedInvocationWhenVerifyingZeroInteractions() { mock.twoArgumentMethod(1, 2); mock.threeArgumentMethod(1, "2", "3"); try { verifyZeroInteractions(mock); fail(); } catch (NoInteractionsWanted e) { String expected = "\n" + "No interactions wanted here:" + "\n" + "-> at"; assertContains(expected, e.getMessage()); String expectedCause = "\n" + "But found this interaction:" + "\n" + "-> at"; assertContains(expectedCause, e.getMessage()); } }
@Test public void should_print_first_unexpected_invocation() { mock.oneArg(true); mock.oneArg(false); mock.threeArgumentMethod(1, "2", "3"); verify(mock).oneArg(true); try { verifyNoMoreInteractions(mock); fail(); } catch (NoInteractionsWanted e) { String expectedMessage = "\n" + "No interactions wanted here:" + "\n" + "-> at"; assertContains(expectedMessage, e.getMessage()); String expectedCause = "\n" + "But found this interaction:" + "\n" + "-> at"; assertContains(expectedCause, e.getMessage()); } }
@Test public void should_print_first_unexpected_invocation_when_verifying_zero_interactions() { mock.twoArgumentMethod(1, 2); mock.threeArgumentMethod(1, "2", "3"); try { verifyZeroInteractions(mock); fail(); } catch (NoInteractionsWanted e) { String expected = "\n" + "No interactions wanted here:" + "\n" + "-> at"; assertContains(expected, e.getMessage()); String expectedCause = "\n" + "But found this interaction:" + "\n" + "-> at"; assertContains(expectedCause, e.getMessage()); } }
@Test public void verifyAdditionalInvocations() { TestClass t = mock(TestClass.class); t.returnA(); t.returnA(); try { verifyNoMoreInteractions(t); } catch (NoInteractionsWanted e) { try { throw new Exception(); } catch (Exception here) { // The error message should indicate where the additional invocations have been made assertTrue(e.getMessage(), e.getMessage().contains(here.getStackTrace()[0].getMethodName())); } } }
@After public void tearDown() { for (Object listener : listeners.values()) { try { // TODO need a way to handle components that have contained components, // like JXComboBox, that cause spurious container events verifyNoMoreInteractions(listener); } catch (NoInteractionsWanted logAndIgnore) { logger.log(Level.WARNING, "unexpected listener notification", logAndIgnore); } } }
/** contains expected only for the test to pass **/ @Test(expectedExceptions = NoInteractionsWanted.class) public void should_fail_on_verifying_no_more_interactions_due_to_existing_stubbing() { // given Person person = new Person(); given(taxService.sendStatisticsReport()).willReturn(true); // when boolean success = systemUnderTest.transferTaxFor(person); // then verify(taxService).transferTaxFor(person); verifyNoMoreInteractions(taxService); then(success).isTrue(); }
/** contains expected only for the test to pass **/ @Test(expected = NoInteractionsWanted.class) public void should_fail_on_verifying_no_more_interactions_due_to_existing_stubbing() { // given Person person = new Person(); given(taxService.sendStatisticsReport()).willReturn(true); // when boolean success = systemUnderTest.transferTaxFor(person); // then verify(taxService).transferTaxFor(person); verifyNoMoreInteractions(taxService); then(success).isTrue(); }
/** contains expected only for the test to pass **/ @Test(expectedExceptions = NoInteractionsWanted.class) public void should_fail_on_verifying_no_more_interactions_due_to_existing_stubbing() { // given Person person = new Person(); given(taxService.sendStatisticsReport()).willReturn(true); // when boolean success = systemUnderTest.transferTaxFor(person); // then verify(taxService).transferTaxFor(person); verifyNoMoreInteractions(taxService); assertThat(success, is(true)); }
/** contains expected only for the test to pass **/ @Test(expected = NoInteractionsWanted.class) public void should_fail_on_verifying_no_more_interactions_due_to_existing_stubbing() { // given Person person = new Person(); given(taxService.sendStatisticsReport()).willReturn(true); // when boolean success = systemUnderTest.transferTaxFor(person); // then verify(taxService).transferTaxFor(person); verifyNoMoreInteractions(taxService); assertThat(success, is(true)); }
public void noMoreInteractionsWanted(Invocation undesired, List<VerificationAwareInvocation> invocations) { ScenarioPrinter scenarioPrinter = new ScenarioPrinter(); String scenario = scenarioPrinter.print(invocations); throw new NoInteractionsWanted(join( "No interactions wanted here:", new Location(), "But found this interaction:", undesired.getLocation(), scenario )); }
@Test public void shouldStubbingBeTreatedAsInteraction() throws Exception { when(mock.booleanReturningMethod()).thenReturn(true); mock.booleanReturningMethod(); try { verifyNoMoreInteractions(mock); fail(); } catch (NoInteractionsWanted e) {} }
@Test public void shouldStubbingBeTreatedAsInteraction() throws Exception { doReturn("foo").when(mock).simpleMethod(); mock.simpleMethod(); try { verifyNoMoreInteractions(mock); fail(); } catch (NoInteractionsWanted e) {} }
@Test public void shouldStubbingBeTreatedAsInteraction() throws Exception { stub(mock.booleanReturningMethod()).toReturn(true); mock.booleanReturningMethod(); try { verifyNoMoreInteractions(mock); fail(); } catch (NoInteractionsWanted e) {} }
@Test public void shouldVerifyObjectVarargs() { mock.withObjectVarargs(1); mock.withObjectVarargs(2, "1", new ArrayList<Object>(), new Integer(1)); mock.withObjectVarargs(3, new Integer(1)); verify(mock).withObjectVarargs(1); verify(mock).withObjectVarargs(2, "1", new ArrayList<Object>(), new Integer(1)); try { verifyNoMoreInteractions(mock); fail(); } catch (NoInteractionsWanted e) {} }
@Test public void shouldFilterStackTraceOnVerifyNoMoreInteractions() { mock.oneArg(true); try { verifyNoMoreInteractions(mock); fail(); } catch (NoInteractionsWanted e) { assertThat(e, hasFirstMethodInStackTrace("shouldFilterStackTraceOnVerifyNoMoreInteractions")); } }
@Test public void shouldFilterStackTraceOnVerifyZeroInteractions() { mock.oneArg(true); try { verifyZeroInteractions(mock); fail(); } catch (NoInteractionsWanted e) { assertThat(e, hasFirstMethodInStackTrace("shouldFilterStackTraceOnVerifyZeroInteractions")); } }
@Test public void shouldFailOnNoMoreInteractions() { inOrder.verify(mockOne, atLeastOnce()).simpleMethod(1); inOrder.verify(mockThree).simpleMethod(3); inOrder.verify(mockThree).simpleMethod(4); try { verifyNoMoreInteractions(mockOne, mockTwo, mockThree); fail(); } catch (NoInteractionsWanted e) {} }
@Test public void shouldFailOnNoMoreInteractionsOnMockVerifiedInOrder() { inOrder.verify(mockOne, atLeastOnce()).simpleMethod(1); inOrder.verify(mockThree).simpleMethod(3); verify(mockTwo).simpleMethod(2); try { verifyNoMoreInteractions(mockOne, mockTwo, mockThree); fail(); } catch (NoInteractionsWanted e) {} }
@Test public void shouldDetectUnverifiedInMarkInteractionsAsVerified() throws Exception { mock.clear(); mock.clear(); undesiredInteraction(); verify(mock, atMost(3)).clear(); try { verifyNoMoreInteractions(mock); fail(); } catch(NoInteractionsWanted e) { assertContains("undesiredInteraction(", e.getMessage()); } }
@Test public void shouldFailZeroInteractionsVerification() throws Exception { mock.clear(); try { verifyZeroInteractions(mock); fail(); } catch (NoInteractionsWanted e) {} }
@Test public void shouldFailNoMoreInteractionsVerification() throws Exception { mock.clear(); try { verifyNoMoreInteractions(mock); fail(); } catch (NoInteractionsWanted e) {} }
@Test public void shouldPrintAllInvocationsWhenVerifyingNoMoreInvocations() throws Exception { mock.add(1); mock.add(2); mock.clear(); verify(mock).add(2); try { verifyNoMoreInteractions(mock); fail(); } catch (NoInteractionsWanted e) { assertContains("list of all invocations", e.getMessage()); } }
@Test public void shouldNotContainAllInvocationsWhenSingleUnwantedFound() throws Exception { mock.add(1); try { verifyNoMoreInteractions(mock); fail(); } catch (NoInteractionsWanted e) { assertNotContains("list of all invocations", e.getMessage()); } }
@Test public void shouldFailIfMethodWasInvokedMoreThanOnce() { mock.clear(); mock.clear(); try { verify(mock, only()).clear(); fail(); } catch (NoInteractionsWanted e) {} }
@Test public void shouldFailIfExtraMethodWithDifferentArgsFound() { mock.get(0); mock.get(2); try { verify(mock, only()).get(2); fail(); } catch (NoInteractionsWanted e) {} }
@Test public void shouldAllowTwoTimesOnMockTwo() { InOrder inOrder = inOrder(mockTwo, mockThree); inOrder.verify(mockTwo, times(2)).simpleMethod(2); try { verifyNoMoreInteractions(mockTwo); fail(); } catch (NoInteractionsWanted e) {} }
@Test public void shouldAllowFirstChunkBeforeLastInvocation() { inOrder.verify(mockTwo, times(2)).simpleMethod(2); inOrder.verify(mockOne).simpleMethod(4); try { verifyNoMoreInteractions(mockTwo); fail(); } catch (NoInteractionsWanted e) {} }
@Test public void shouldVerifyDetectFirstChunkOfInvocationThatExistInManyChunks() { inOrder.verify(mockTwo, times(2)).simpleMethod(2); inOrder.verify(mockThree).simpleMethod(3); try { verifyNoMoreInteractions(mockTwo); fail(); } catch(NoInteractionsWanted e) {} }
@Test public void shouldVerifyInteractionsFromFirstChunk() { inOrder.verify(mockTwo, times(2)).simpleMethod(2); try { verifyNoMoreInteractions(mockTwo); fail(); } catch (NoInteractionsWanted e) {} }
@Test public void shouldFailOnVerifyNoMoreInteractions() { inOrder.verify(mockOne).simpleMethod(1); inOrder.verify(mockTwo, times(2)).simpleMethod(2); inOrder.verify(mockThree).simpleMethod(3); inOrder.verify(mockTwo).simpleMethod(2); try { verifyNoMoreInteractions(mockOne, mockTwo, mockThree); fail(); } catch (NoInteractionsWanted e) { } }
@Test public void shouldDetectRedundantInvocation() throws Exception { mock.clear(); mock.add("foo"); mock.add("bar"); verify(mock).clear(); verify(mock).add("foo"); try { verifyNoMoreInteractions(mock); fail(); } catch (NoInteractionsWanted e) {} }
@Test public void shouldVerifyNoMoreInteractionsAndFail() { spy.add("one"); spy.add("two"); verify(spy).add("one"); try { verifyNoMoreInteractions(spy); fail(); } catch (NoInteractionsWanted e) {} }
public void noMoreInteractionsWanted(Invocation undesired, List<VerificationAwareInvocation> invocations) { ScenarioPrinter scenarioPrinter = new ScenarioPrinter(); String scenario = scenarioPrinter.print(invocations); throw new NoInteractionsWanted(join( "No interactions wanted here:", new LocationImpl(), "But found this interaction:", undesired.getLocation(), scenario )); }
@Test public void should_stubbing_be_treated_as_interaction() throws Exception { when(mock.booleanReturningMethod()).thenReturn(true); mock.booleanReturningMethod(); try { verifyNoMoreInteractions(mock); fail(); } catch (NoInteractionsWanted e) {} }