further examples and doc work

This commit is contained in:
eugenp 2013-11-06 17:02:13 +02:00
parent 311e86fe25
commit ae6082ccec
2 changed files with 93 additions and 59 deletions

View File

@ -1,8 +1,8 @@
========= =========
## Guava Cookbooks and Examples ## Mockito Cookbooks and Examples
### Relevant Articles: ### Relevant Articles:
- [Guava Collections Cookbook](http://www.baeldung.com/guava-collections) - [Mockito Verify Cookbook](http://www.baeldung.com/mockito-verify)

View File

@ -1,5 +1,8 @@
package org.baeldung.mockito; 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.atLeast;
import static org.mockito.Mockito.atMost; import static org.mockito.Mockito.atMost;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
@ -12,80 +15,111 @@ import static org.mockito.Mockito.verifyZeroInteractions;
import java.util.List; import java.util.List;
import org.junit.Test; import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.InOrder; import org.mockito.InOrder;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.mockito.exceptions.verification.NoInteractionsWanted; import org.mockito.exceptions.verification.NoInteractionsWanted;
import com.google.common.collect.Lists;
public class MockitoVerifyExamplesTest { public class MockitoVerifyExamplesTest {
// tests // tests
@Test @Test
public final void givenInteractionWithMockOccurred_whenVerifyingInteraction_thenCorrect() { public final void givenInteractionWithMockOccurred_whenVerifyingInteraction_thenCorrect() {
final List<String> mockedList = mock(MyList.class); final List<String> mockedList = mock(MyList.class);
mockedList.size(); mockedList.size();
verify(mockedList).size(); verify(mockedList).size();
} }
@Test @Test
public final void givenOneInteractionWithMockOccurred_whenVerifyingNumberOfInteractions_thenCorrect() { public final void givenOneInteractionWithMockOccurred_whenVerifyingNumberOfInteractions_thenCorrect() {
final List<String> mockedList = mock(MyList.class); final List<String> mockedList = mock(MyList.class);
mockedList.size(); mockedList.size();
verify(mockedList, times(1)).size(); verify(mockedList, times(1)).size();
} }
@Test @Test
public final void givenNoInteractionWithMockOccurred_whenVerifyingInteractions_thenCorrect() { public final void givenNoInteractionWithMockOccurred_whenVerifyingInteractions_thenCorrect() {
final List<String> mockedList = mock(MyList.class); final List<String> mockedList = mock(MyList.class);
verifyZeroInteractions(mockedList); verifyZeroInteractions(mockedList);
} }
@Test @Test
public final void givenNoInteractionWithMethodOfMockOccurred_whenVerifyingInteractions_thenCorrect() { public final void givenNoInteractionWithMethodOfMockOccurred_whenVerifyingInteractions_thenCorrect() {
final List<String> mockedList = mock(MyList.class); final List<String> mockedList = mock(MyList.class);
verify(mockedList, times(0)).size(); verify(mockedList, times(0)).size();
} }
@Test(expected = NoInteractionsWanted.class) @Test(expected = NoInteractionsWanted.class)
public final void givenUnverifiedInteraction_whenVerifyingNoUnexpectedInteractions_thenFail() { public final void givenUnverifiedInteraction_whenVerifyingNoUnexpectedInteractions_thenFail() {
final List<String> mockedList = mock(MyList.class); final List<String> mockedList = mock(MyList.class);
mockedList.size(); mockedList.size();
mockedList.clear(); mockedList.clear();
verify(mockedList).size(); verify(mockedList).size();
verifyNoMoreInteractions(mockedList); verifyNoMoreInteractions(mockedList);
} }
@Test @Test
public final void whenVerifyingOrderOfInteractions_thenCorrect() { public final void whenVerifyingOrderOfInteractions_thenCorrect() {
final List<String> mockedList = mock(MyList.class); final List<String> mockedList = mock(MyList.class);
mockedList.size(); mockedList.size();
mockedList.add("a parameter"); mockedList.add("a parameter");
mockedList.clear(); mockedList.clear();
final InOrder inOrder = Mockito.inOrder(mockedList); final InOrder inOrder = Mockito.inOrder(mockedList);
inOrder.verify(mockedList).size(); inOrder.verify(mockedList).size();
inOrder.verify(mockedList).add("a parameter"); inOrder.verify(mockedList).add("a parameter");
inOrder.verify(mockedList).clear(); inOrder.verify(mockedList).clear();
} }
@Test @Test
public final void whenVerifyingAnInteractionHasNotOccurred_thenCorrect() { public final void whenVerifyingAnInteractionHasNotOccurred_thenCorrect() {
final List<String> mockedList = mock(MyList.class); final List<String> mockedList = mock(MyList.class);
mockedList.size(); mockedList.size();
verify(mockedList, never()).clear(); verify(mockedList, never()).clear();
} }
@Test @Test
public final void whenVerifyingAnInteractionHasOccurredAtLeastOnce_thenCorrect() { public final void whenVerifyingAnInteractionHasOccurredAtLeastOnce_thenCorrect() {
final List<String> mockedList = mock(MyList.class); final List<String> mockedList = mock(MyList.class);
mockedList.clear(); mockedList.clear();
mockedList.clear(); mockedList.clear();
mockedList.clear(); mockedList.clear();
verify(mockedList, atLeast(1)).clear(); verify(mockedList, atLeast(1)).clear();
verify(mockedList, atMost(10)).clear(); verify(mockedList, atMost(10)).clear();
} }
// with arguments
@Test
public final void whenVerifyingAnInteractionWithExactArgument_thenCorrect() {
final List<String> mockedList = mock(MyList.class);
mockedList.add("test");
verify(mockedList).add("test");
}
@Test
public final void whenVerifyingAnInteractionWithAnyArgument_thenCorrect() {
final List<String> mockedList = mock(MyList.class);
mockedList.add("test");
verify(mockedList).add(anyString());
}
@Test
public final void whenVerifyingAnInteractionWithArgumentCapture_thenCorrect() {
final List<String> mockedList = mock(MyList.class);
mockedList.addAll(Lists.<String> newArrayList("someElement"));
final ArgumentCaptor<List> argumentCaptor = ArgumentCaptor.forClass(List.class);
verify(mockedList).addAll(argumentCaptor.capture());
final List<String> capturedArgument = argumentCaptor.<List<String>> getValue();
assertThat(capturedArgument, hasItem("someElement"));
}
} }