further examples and doc work
This commit is contained in:
parent
311e86fe25
commit
ae6082ccec
|
@ -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)
|
||||||
|
|
||||||
|
|
|
@ -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,10 +15,13 @@ 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
|
||||||
|
@ -88,4 +94,32 @@ public class MockitoVerifyExamplesTest {
|
||||||
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"));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue