From ae6082ccec9c54144e4bb7c1e29b5e90fc46f469 Mon Sep 17 00:00:00 2001 From: eugenp Date: Wed, 6 Nov 2013 17:02:13 +0200 Subject: [PATCH] further examples and doc work --- mockito/README.md | 4 +- .../mockito/MockitoVerifyExamplesTest.java | 148 +++++++++++------- 2 files changed, 93 insertions(+), 59 deletions(-) diff --git a/mockito/README.md b/mockito/README.md index d6d3ef275d..3479832586 100644 --- a/mockito/README.md +++ b/mockito/README.md @@ -1,8 +1,8 @@ ========= -## Guava Cookbooks and Examples +## Mockito Cookbooks and Examples ### Relevant Articles: -- [Guava Collections Cookbook](http://www.baeldung.com/guava-collections) +- [Mockito Verify Cookbook](http://www.baeldung.com/mockito-verify) diff --git a/mockito/src/test/java/org/baeldung/mockito/MockitoVerifyExamplesTest.java b/mockito/src/test/java/org/baeldung/mockito/MockitoVerifyExamplesTest.java index b2502d50fe..6875a9f819 100644 --- a/mockito/src/test/java/org/baeldung/mockito/MockitoVerifyExamplesTest.java +++ b/mockito/src/test/java/org/baeldung/mockito/MockitoVerifyExamplesTest.java @@ -1,5 +1,8 @@ package org.baeldung.mockito; +import static org.hamcrest.Matchers.hasItem; +import static org.junit.Assert.assertThat; +import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.atLeast; import static org.mockito.Mockito.atMost; import static org.mockito.Mockito.mock; @@ -12,80 +15,111 @@ import static org.mockito.Mockito.verifyZeroInteractions; import java.util.List; import org.junit.Test; +import org.mockito.ArgumentCaptor; import org.mockito.InOrder; import org.mockito.Mockito; import org.mockito.exceptions.verification.NoInteractionsWanted; +import com.google.common.collect.Lists; + public class MockitoVerifyExamplesTest { - // tests + // tests - @Test - public final void givenInteractionWithMockOccurred_whenVerifyingInteraction_thenCorrect() { - final List mockedList = mock(MyList.class); - mockedList.size(); - verify(mockedList).size(); - } + @Test + public final void givenInteractionWithMockOccurred_whenVerifyingInteraction_thenCorrect() { + final List mockedList = mock(MyList.class); + mockedList.size(); + verify(mockedList).size(); + } - @Test - public final void givenOneInteractionWithMockOccurred_whenVerifyingNumberOfInteractions_thenCorrect() { - final List mockedList = mock(MyList.class); - mockedList.size(); - verify(mockedList, times(1)).size(); - } + @Test + public final void givenOneInteractionWithMockOccurred_whenVerifyingNumberOfInteractions_thenCorrect() { + final List mockedList = mock(MyList.class); + mockedList.size(); + verify(mockedList, times(1)).size(); + } - @Test - public final void givenNoInteractionWithMockOccurred_whenVerifyingInteractions_thenCorrect() { - final List mockedList = mock(MyList.class); - verifyZeroInteractions(mockedList); - } + @Test + public final void givenNoInteractionWithMockOccurred_whenVerifyingInteractions_thenCorrect() { + final List mockedList = mock(MyList.class); + verifyZeroInteractions(mockedList); + } - @Test - public final void givenNoInteractionWithMethodOfMockOccurred_whenVerifyingInteractions_thenCorrect() { - final List mockedList = mock(MyList.class); - verify(mockedList, times(0)).size(); - } + @Test + public final void givenNoInteractionWithMethodOfMockOccurred_whenVerifyingInteractions_thenCorrect() { + final List mockedList = mock(MyList.class); + verify(mockedList, times(0)).size(); + } - @Test(expected = NoInteractionsWanted.class) - public final void givenUnverifiedInteraction_whenVerifyingNoUnexpectedInteractions_thenFail() { - final List mockedList = mock(MyList.class); - mockedList.size(); - mockedList.clear(); + @Test(expected = NoInteractionsWanted.class) + public final void givenUnverifiedInteraction_whenVerifyingNoUnexpectedInteractions_thenFail() { + final List mockedList = mock(MyList.class); + mockedList.size(); + mockedList.clear(); - verify(mockedList).size(); - verifyNoMoreInteractions(mockedList); - } + verify(mockedList).size(); + verifyNoMoreInteractions(mockedList); + } - @Test - public final void whenVerifyingOrderOfInteractions_thenCorrect() { - final List mockedList = mock(MyList.class); - mockedList.size(); - mockedList.add("a parameter"); - mockedList.clear(); + @Test + public final void whenVerifyingOrderOfInteractions_thenCorrect() { + final List mockedList = mock(MyList.class); + mockedList.size(); + mockedList.add("a parameter"); + mockedList.clear(); - final InOrder inOrder = Mockito.inOrder(mockedList); - inOrder.verify(mockedList).size(); - inOrder.verify(mockedList).add("a parameter"); - inOrder.verify(mockedList).clear(); - } + final InOrder inOrder = Mockito.inOrder(mockedList); + inOrder.verify(mockedList).size(); + inOrder.verify(mockedList).add("a parameter"); + inOrder.verify(mockedList).clear(); + } - @Test - public final void whenVerifyingAnInteractionHasNotOccurred_thenCorrect() { - final List mockedList = mock(MyList.class); - mockedList.size(); + @Test + public final void whenVerifyingAnInteractionHasNotOccurred_thenCorrect() { + final List mockedList = mock(MyList.class); + mockedList.size(); - verify(mockedList, never()).clear(); - } + verify(mockedList, never()).clear(); + } - @Test - public final void whenVerifyingAnInteractionHasOccurredAtLeastOnce_thenCorrect() { - final List mockedList = mock(MyList.class); - mockedList.clear(); - mockedList.clear(); - mockedList.clear(); + @Test + public final void whenVerifyingAnInteractionHasOccurredAtLeastOnce_thenCorrect() { + final List mockedList = mock(MyList.class); + mockedList.clear(); + mockedList.clear(); + mockedList.clear(); - verify(mockedList, atLeast(1)).clear(); - verify(mockedList, atMost(10)).clear(); - } + verify(mockedList, atLeast(1)).clear(); + verify(mockedList, atMost(10)).clear(); + } + + // with arguments + + @Test + public final void whenVerifyingAnInteractionWithExactArgument_thenCorrect() { + final List mockedList = mock(MyList.class); + mockedList.add("test"); + + verify(mockedList).add("test"); + } + + @Test + public final void whenVerifyingAnInteractionWithAnyArgument_thenCorrect() { + final List mockedList = mock(MyList.class); + mockedList.add("test"); + + verify(mockedList).add(anyString()); + } + + @Test + public final void whenVerifyingAnInteractionWithArgumentCapture_thenCorrect() { + final List mockedList = mock(MyList.class); + mockedList.addAll(Lists. newArrayList("someElement")); + final ArgumentCaptor argumentCaptor = ArgumentCaptor.forClass(List.class); + verify(mockedList).addAll(argumentCaptor.capture()); + final List capturedArgument = argumentCaptor.> getValue(); + assertThat(capturedArgument, hasItem("someElement")); + } }