diff --git a/mockito/src/test/java/org/baeldung/mockito/MockitoVerifyExamplesTest.java b/mockito/src/test/java/org/baeldung/mockito/MockitoVerifyExamplesTest.java index 001c055b46..b2502d50fe 100644 --- a/mockito/src/test/java/org/baeldung/mockito/MockitoVerifyExamplesTest.java +++ b/mockito/src/test/java/org/baeldung/mockito/MockitoVerifyExamplesTest.java @@ -1,14 +1,91 @@ package org.baeldung.mockito; +import static org.mockito.Mockito.atLeast; +import static org.mockito.Mockito.atMost; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.verifyZeroInteractions; + +import java.util.List; + import org.junit.Test; +import org.mockito.InOrder; +import org.mockito.Mockito; +import org.mockito.exceptions.verification.NoInteractionsWanted; public class MockitoVerifyExamplesTest { // tests @Test - public final void when_thenCorrect() { - // + 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 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(expected = NoInteractionsWanted.class) + public final void givenUnverifiedInteraction_whenVerifyingNoUnexpectedInteractions_thenFail() { + final List mockedList = mock(MyList.class); + mockedList.size(); + mockedList.clear(); + + 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(); + + 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(); + + verify(mockedList, never()).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(); } } diff --git a/mockito/src/test/java/org/baeldung/mockito/MyList.java b/mockito/src/test/java/org/baeldung/mockito/MyList.java new file mode 100644 index 0000000000..23a8f65e6c --- /dev/null +++ b/mockito/src/test/java/org/baeldung/mockito/MyList.java @@ -0,0 +1,17 @@ +package org.baeldung.mockito; + +import java.util.AbstractList; + +public class MyList extends AbstractList { + + @Override + public String get(final int index) { + return null; + } + + @Override + public int size() { + return 0; + } + +}