JAVA-12032 Move Mockito ebook articles code to common module - mockito-simple- Delete mockito-3 since it had only 1 article which was moved to mockito-simple

This commit is contained in:
Dhawal Kapil
2022-07-15 17:52:21 +05:30
parent 3860716e20
commit 24d4feb541
60 changed files with 111 additions and 435 deletions
@@ -1,10 +0,0 @@
package com.baeldung.mockito;
public final class FinalList extends MyList {
@Override
public int size() {
return 1;
}
}
@@ -1,35 +0,0 @@
package com.baeldung.mockito;
import org.junit.Test;
import static org.junit.Assert.assertNotEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class MockFinals {
@Test
public void whenMockFinalClassMockWorks() {
FinalList finalList = new FinalList();
FinalList mock = mock(FinalList.class);
when(mock.size()).thenReturn(2);
assertNotEquals(mock.size(), finalList.size());
}
@Test
public void whenMockFinalMethodMockWorks() {
MyList myList = new MyList();
MyList mock = mock(MyList.class);
when(mock.finalMethod()).thenReturn(1);
assertNotEquals(mock.finalMethod(), myList.finalMethod());
}
}
@@ -1,37 +0,0 @@
package com.baeldung.mockito;
import org.junit.Test;
import static org.junit.Assert.assertNotEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import com.baeldung.mockito.MyList;
public class MockFinalsUnitTest {
@Test
public void whenMockFinalClassMockWorks() {
FinalList finalList = new FinalList();
FinalList mock = mock(FinalList.class);
when(mock.size()).thenReturn(2);
assertNotEquals(mock.size(), finalList.size());
}
@Test
public void whenMockFinalMethodMockWorks() {
MyList myList = new MyList();
MyList mock = mock(MyList.class);
when(mock.finalMethod()).thenReturn(1);
assertNotEquals(mock.finalMethod(), myList.finalMethod());
}
}
@@ -1,114 +0,0 @@
package com.baeldung.mockito;
import org.junit.Before;
import org.junit.Test;
import org.mockito.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import static org.junit.Assert.assertEquals;
//@RunWith(MockitoJUnitRunner.class)
public class MockitoAnnotationIntegrationTest {
@Mock
private List<String> mockedList;
@Spy
private List<String> spiedList = new ArrayList<>();
@Before
public void init() {
MockitoAnnotations.initMocks(this);
}
// tests
@Test
public void whenNotUseMockAnnotation_thenCorrect() {
final List<String> mockList = Mockito.mock(List.class);
mockList.add("one");
Mockito.verify(mockList).add("one");
assertEquals(0, mockList.size());
Mockito.when(mockList.size()).thenReturn(100);
assertEquals(100, mockList.size());
}
@Test
public void whenUseMockAnnotation_thenMockIsInjected() {
mockedList.add("one");
Mockito.verify(mockedList).add("one");
assertEquals(0, mockedList.size());
Mockito.when(mockedList.size()).thenReturn(100);
assertEquals(100, mockedList.size());
}
@Test
public void whenNotUseSpyAnnotation_thenCorrect() {
final List<String> spyList = Mockito.spy(new ArrayList<String>());
spyList.add("one");
spyList.add("two");
Mockito.verify(spyList).add("one");
Mockito.verify(spyList).add("two");
assertEquals(2, spyList.size());
Mockito.doReturn(100).when(spyList).size();
assertEquals(100, spyList.size());
}
@Test
public void whenUseSpyAnnotation_thenSpyIsInjectedCorrectly() {
spiedList.add("one");
spiedList.add("two");
Mockito.verify(spiedList).add("one");
Mockito.verify(spiedList).add("two");
assertEquals(2, spiedList.size());
Mockito.doReturn(100).when(spiedList).size();
assertEquals(100, spiedList.size());
}
@Test
public void whenNotUseCaptorAnnotation_thenCorrect() {
final List<String> mockList = Mockito.mock(List.class);
final ArgumentCaptor<String> arg = ArgumentCaptor.forClass(String.class);
mockList.add("one");
Mockito.verify(mockList).add(arg.capture());
assertEquals("one", arg.getValue());
}
@Captor
private
ArgumentCaptor<String> argCaptor;
@Test
public void whenUseCaptorAnnotation_thenTheSam() {
mockedList.add("one");
Mockito.verify(mockedList).add(argCaptor.capture());
assertEquals("one", argCaptor.getValue());
}
@Mock
private Map<String, String> wordMap;
@InjectMocks
private MyDictionary dic = new MyDictionary();
@Test
public void whenUseInjectMocksAnnotation_thenCorrect() {
Mockito.when(wordMap.get("aWord")).thenReturn("aMeaning");
assertEquals("aMeaning", dic.getMeaning("aWord"));
}
}
@@ -1,114 +0,0 @@
package com.baeldung.mockito;
import org.junit.Before;
import org.junit.Test;
import org.mockito.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import static org.junit.Assert.assertEquals;
//@RunWith(MockitoJUnitRunner.class)
public class MockitoAnnotationUnitTest {
@Mock
private List<String> mockedList;
@Spy
private List<String> spiedList = new ArrayList<>();
@Before
public void init() {
MockitoAnnotations.initMocks(this);
}
// tests
@Test
public void whenNotUseMockAnnotation_thenCorrect() {
final List<String> mockList = Mockito.mock(List.class);
mockList.add("one");
Mockito.verify(mockList).add("one");
assertEquals(0, mockList.size());
Mockito.when(mockList.size()).thenReturn(100);
assertEquals(100, mockList.size());
}
@Test
public void whenUseMockAnnotation_thenMockIsInjected() {
mockedList.add("one");
Mockito.verify(mockedList).add("one");
assertEquals(0, mockedList.size());
Mockito.when(mockedList.size()).thenReturn(100);
assertEquals(100, mockedList.size());
}
@Test
public void whenNotUseSpyAnnotation_thenCorrect() {
final List<String> spyList = Mockito.spy(new ArrayList<String>());
spyList.add("one");
spyList.add("two");
Mockito.verify(spyList).add("one");
Mockito.verify(spyList).add("two");
assertEquals(2, spyList.size());
Mockito.doReturn(100).when(spyList).size();
assertEquals(100, spyList.size());
}
@Test
public void whenUseSpyAnnotation_thenSpyIsInjectedCorrectly() {
spiedList.add("one");
spiedList.add("two");
Mockito.verify(spiedList).add("one");
Mockito.verify(spiedList).add("two");
assertEquals(2, spiedList.size());
Mockito.doReturn(100).when(spiedList).size();
assertEquals(100, spiedList.size());
}
@Test
public void whenNotUseCaptorAnnotation_thenCorrect() {
final List<String> mockList = Mockito.mock(List.class);
final ArgumentCaptor<String> arg = ArgumentCaptor.forClass(String.class);
mockList.add("one");
Mockito.verify(mockList).add(arg.capture());
assertEquals("one", arg.getValue());
}
@Captor
private
ArgumentCaptor<String> argCaptor;
@Test
public void whenUseCaptorAnnotation_thenTheSam() {
mockedList.add("one");
Mockito.verify(mockedList).add(argCaptor.capture());
assertEquals("one", argCaptor.getValue());
}
@Mock
private Map<String, String> wordMap;
@InjectMocks
private MyDictionary dic = new MyDictionary();
@Test
public void whenUseInjectMocksAnnotation_thenCorrect() {
Mockito.when(wordMap.get("aWord")).thenReturn("aMeaning");
assertEquals("aMeaning", dic.getMeaning("aWord"));
}
}
@@ -1,18 +0,0 @@
package com.baeldung.mockito;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import java.util.List;
public class MockitoAnnotationsUninitializedUnitTest {
@Mock
List<String> mockedList;
@Test(expected = NullPointerException.class)
public void whenMockitoAnnotationsUninitialized_thenNPEThrown() {
Mockito.when(mockedList.size()).thenReturn(1);
}
}
@@ -1,93 +0,0 @@
package com.baeldung.mockito;
import org.junit.Test;
import org.mockito.Mockito;
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.*;
public class MockitoConfigExamplesIntegrationTest {
// tests
@Test
public final void whenMockReturnBehaviorIsConfigured_thenBehaviorIsVerified() {
final MyList listMock = Mockito.mock(MyList.class);
when(listMock.add(anyString())).thenReturn(false);
final boolean added = listMock.add(randomAlphabetic(6));
assertThat(added, is(false));
}
@Test
public final void whenMockReturnBehaviorIsConfigured2_thenBehaviorIsVerified() {
final MyList listMock = Mockito.mock(MyList.class);
doReturn(false).when(listMock).add(anyString());
final boolean added = listMock.add(randomAlphabetic(6));
assertThat(added, is(false));
}
@Test(expected = IllegalStateException.class)
public final void givenMethodIsConfiguredToThrowException_whenCallingMethod_thenExceptionIsThrown() {
final MyList listMock = Mockito.mock(MyList.class);
when(listMock.add(anyString())).thenThrow(IllegalStateException.class);
listMock.add(randomAlphabetic(6));
}
@Test(expected = NullPointerException.class)
public final void whenMethodHasNoReturnType_whenConfiguringBehaviorOfMethod_thenPossible() {
final MyList listMock = Mockito.mock(MyList.class);
doThrow(NullPointerException.class).when(listMock).clear();
listMock.clear();
}
@Test
public final void givenBehaviorIsConfiguredToThrowExceptionOnSecondCall_whenCallingOnlyOnce_thenNoExceptionIsThrown() {
final MyList listMock = Mockito.mock(MyList.class);
when(listMock.add(anyString())).thenReturn(false).thenThrow(IllegalStateException.class);
listMock.add(randomAlphabetic(6));
}
@Test(expected = IllegalStateException.class)
public final void givenBehaviorIsConfiguredToThrowExceptionOnSecondCall_whenCallingTwice_thenExceptionIsThrown() {
final MyList listMock = Mockito.mock(MyList.class);
when(listMock.add(anyString())).thenReturn(false).thenThrow(IllegalStateException.class);
listMock.add(randomAlphabetic(6));
listMock.add(randomAlphabetic(6));
}
@Test
public final void whenMockMethodCallIsConfiguredToCallTheRealMethod_thenRealMethodIsCalled() {
final MyList listMock = Mockito.mock(MyList.class);
when(listMock.size()).thenCallRealMethod();
assertThat(listMock.size(), equalTo(1));
}
@Test
public final void whenMockMethodCallIsConfiguredWithCustomAnswer_thenRealMethodIsCalled() {
final MyList listMock = Mockito.mock(MyList.class);
doAnswer(invocation -> "Always the same").when(listMock).get(anyInt());
final String element = listMock.get(1);
assertThat(element, is(equalTo("Always the same")));
}
@Test(expected = NullPointerException.class)
public final void givenSpy_whenConfiguringBehaviorOfSpy_thenCorrectlyConfigured() {
final MyList instance = new MyList();
final MyList spy = Mockito.spy(instance);
doThrow(NullPointerException.class).when(spy).size();
spy.size();
}
}
@@ -1,95 +0,0 @@
package com.baeldung.mockito;
import org.junit.Test;
import org.mockito.Mockito;
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.*;
import com.baeldung.mockito.MyList;
public class MockitoConfigExamplesUnitTest {
// tests
@Test
public final void whenMockReturnBehaviorIsConfigured_thenBehaviorIsVerified() {
final MyList listMock = Mockito.mock(MyList.class);
when(listMock.add(anyString())).thenReturn(false);
final boolean added = listMock.add(randomAlphabetic(6));
assertThat(added, is(false));
}
@Test
public final void whenMockReturnBehaviorIsConfigured2_thenBehaviorIsVerified() {
final MyList listMock = Mockito.mock(MyList.class);
doReturn(false).when(listMock).add(anyString());
final boolean added = listMock.add(randomAlphabetic(6));
assertThat(added, is(false));
}
@Test(expected = IllegalStateException.class)
public final void givenMethodIsConfiguredToThrowException_whenCallingMethod_thenExceptionIsThrown() {
final MyList listMock = Mockito.mock(MyList.class);
when(listMock.add(anyString())).thenThrow(IllegalStateException.class);
listMock.add(randomAlphabetic(6));
}
@Test(expected = NullPointerException.class)
public final void whenMethodHasNoReturnType_whenConfiguringBehaviorOfMethod_thenPossible() {
final MyList listMock = Mockito.mock(MyList.class);
doThrow(NullPointerException.class).when(listMock).clear();
listMock.clear();
}
@Test
public final void givenBehaviorIsConfiguredToThrowExceptionOnSecondCall_whenCallingOnlyOnce_thenNoExceptionIsThrown() {
final MyList listMock = Mockito.mock(MyList.class);
when(listMock.add(anyString())).thenReturn(false).thenThrow(IllegalStateException.class);
listMock.add(randomAlphabetic(6));
}
@Test(expected = IllegalStateException.class)
public final void givenBehaviorIsConfiguredToThrowExceptionOnSecondCall_whenCallingTwice_thenExceptionIsThrown() {
final MyList listMock = Mockito.mock(MyList.class);
when(listMock.add(anyString())).thenReturn(false).thenThrow(IllegalStateException.class);
listMock.add(randomAlphabetic(6));
listMock.add(randomAlphabetic(6));
}
@Test
public final void whenMockMethodCallIsConfiguredToCallTheRealMethod_thenRealMethodIsCalled() {
final MyList listMock = Mockito.mock(MyList.class);
when(listMock.size()).thenCallRealMethod();
assertThat(listMock.size(), equalTo(1));
}
@Test
public final void whenMockMethodCallIsConfiguredWithCustomAnswer_thenRealMethodIsCalled() {
final MyList listMock = Mockito.mock(MyList.class);
doAnswer(invocation -> "Always the same").when(listMock).get(anyInt());
final String element = listMock.get(1);
assertThat(element, is(equalTo("Always the same")));
}
@Test(expected = NullPointerException.class)
public final void givenSpy_whenConfiguringBehaviorOfSpy_thenCorrectlyConfigured() {
final MyList instance = new MyList();
final MyList spy = Mockito.spy(instance);
doThrow(NullPointerException.class).when(spy).size();
spy.size();
}
}
@@ -1,55 +0,0 @@
package com.baeldung.mockito;
import org.junit.Test;
import org.mockito.Mockito;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.*;
public class MockitoExceptionIntegrationTest {
@Test(expected = NullPointerException.class)
public void whenConfigNonVoidRetunMethodToThrowEx_thenExIsThrown() {
MyDictionary dictMock = mock(MyDictionary.class);
when(dictMock.getMeaning(anyString())).thenThrow(NullPointerException.class);
dictMock.getMeaning("word");
}
@Test(expected = IllegalStateException.class)
public void whenConfigVoidRetunMethodToThrowEx_thenExIsThrown() {
MyDictionary dictMock = mock(MyDictionary.class);
doThrow(IllegalStateException.class).when(dictMock)
.add(anyString(), anyString());
dictMock.add("word", "meaning");
}
@Test(expected = NullPointerException.class)
public void whenConfigNonVoidRetunMethodToThrowExWithNewExObj_thenExIsThrown() {
MyDictionary dictMock = mock(MyDictionary.class);
when(dictMock.getMeaning(anyString())).thenThrow(new NullPointerException("Error occurred"));
dictMock.getMeaning("word");
}
@Test(expected = IllegalStateException.class)
public void whenConfigVoidRetunMethodToThrowExWithNewExObj_thenExIsThrown() {
MyDictionary dictMock = mock(MyDictionary.class);
doThrow(new IllegalStateException("Error occurred")).when(dictMock)
.add(anyString(), anyString());
dictMock.add("word", "meaning");
}
// =====
@Test(expected = NullPointerException.class)
public void givenSpy_whenConfigNonVoidRetunMethodToThrowEx_thenExIsThrown() {
MyDictionary dict = new MyDictionary();
MyDictionary spy = Mockito.spy(dict);
when(spy.getMeaning(anyString())).thenThrow(NullPointerException.class);
spy.getMeaning("word");
}
}
@@ -1,57 +0,0 @@
package com.baeldung.mockito;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.junit.Test;
import org.mockito.Mockito;
public class MockitoExceptionUnitTest {
@Test(expected = NullPointerException.class)
public void whenConfigNonVoidRetunMethodToThrowEx_thenExIsThrown() {
MyDictionary dictMock = mock(MyDictionary.class);
when(dictMock.getMeaning(anyString())).thenThrow(NullPointerException.class);
dictMock.getMeaning("word");
}
@Test(expected = IllegalStateException.class)
public void whenConfigVoidRetunMethodToThrowEx_thenExIsThrown() {
MyDictionary dictMock = mock(MyDictionary.class);
doThrow(IllegalStateException.class).when(dictMock)
.add(anyString(), anyString());
dictMock.add("word", "meaning");
}
@Test(expected = NullPointerException.class)
public void whenConfigNonVoidRetunMethodToThrowExWithNewExObj_thenExIsThrown() {
MyDictionary dictMock = mock(MyDictionary.class);
when(dictMock.getMeaning(anyString())).thenThrow(new NullPointerException("Error occurred"));
dictMock.getMeaning("word");
}
@Test(expected = IllegalStateException.class)
public void whenConfigVoidRetunMethodToThrowExWithNewExObj_thenExIsThrown() {
MyDictionary dictMock = mock(MyDictionary.class);
doThrow(new IllegalStateException("Error occurred")).when(dictMock)
.add(anyString(), anyString());
dictMock.add("word", "meaning");
}
// =====
@Test(expected = NullPointerException.class)
public void givenSpy_whenConfigNonVoidRetunMethodToThrowEx_thenExIsThrown() {
MyDictionary dict = new MyDictionary();
MyDictionary spy = Mockito.spy(dict);
when(spy.getMeaning(anyString())).thenThrow(NullPointerException.class);
spy.getMeaning("word");
}
}
@@ -1,28 +0,0 @@
package com.baeldung.mockito;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;
public class MockitoInitWithMockitoJUnitRuleUnitTest {
@Rule
public MockitoRule initRule = MockitoJUnit.rule();
@Mock
private List<String> mockedList;
@Test
public void whenUsingMockitoJUnitRule_thenMocksInitialized() {
when(mockedList.size()).thenReturn(41);
assertThat(mockedList.size()).isEqualTo(41);
}
}
@@ -1,37 +0,0 @@
package com.baeldung.mockito;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.*;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Map;
import static org.junit.Assert.assertEquals;
@RunWith(MockitoJUnitRunner.class)
public class MockitoInjectIntoSpyUnitTest {
@Before
public void init() {
MockitoAnnotations.initMocks(this);
spyDic = Mockito.spy(new MyDictionary(wordMap));
}
@Mock
private Map<String, String> wordMap;
@InjectMocks
private MyDictionary dic = new MyDictionary();
private MyDictionary spyDic;
@Test
public void whenUseInjectMocksAnnotation_thenCorrect() {
Mockito.when(wordMap.get("aWord")).thenReturn("aMeaning");
assertEquals("aMeaning", spyDic.getMeaning("aWord"));
}
}
@@ -1,69 +0,0 @@
package com.baeldung.mockito;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.MockSettings;
import org.mockito.exceptions.verification.TooFewActualInvocations;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.*;
public class MockitoMockIntegrationTest {
private static class CustomAnswer implements Answer<Boolean> {
@Override
public Boolean answer(InvocationOnMock invocation) throws Throwable {
return false;
}
}
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void whenUsingSimpleMock_thenCorrect() {
MyList listMock = mock(MyList.class);
when(listMock.add(anyString())).thenReturn(false);
boolean added = listMock.add(randomAlphabetic(6));
verify(listMock).add(anyString());
assertThat(added, is(false));
}
@Test
public void whenUsingMockWithName_thenCorrect() {
MyList listMock = mock(MyList.class, "myMock");
when(listMock.add(anyString())).thenReturn(false);
listMock.add(randomAlphabetic(6));
thrown.expect(TooFewActualInvocations.class);
thrown.expectMessage(containsString("myMock.add"));
verify(listMock, times(2)).add(anyString());
}
@Test
public void whenUsingMockWithAnswer_thenCorrect() {
MyList listMock = mock(MyList.class, new CustomAnswer());
boolean added = listMock.add(randomAlphabetic(6));
verify(listMock).add(anyString());
assertThat(added, is(false));
}
@Test
public void whenUsingMockWithSettings_thenCorrect() {
MockSettings customSettings = withSettings().defaultAnswer(new CustomAnswer());
MyList listMock = mock(MyList.class, customSettings);
boolean added = listMock.add(randomAlphabetic(6));
verify(listMock).add(anyString());
assertThat(added, is(false));
}
}
@@ -1,70 +0,0 @@
package com.baeldung.mockito;
import static org.mockito.Mockito.*;
import static org.junit.Assert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.containsString;
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
import org.junit.Test;
import org.junit.Rule;
import org.junit.rules.ExpectedException;
import org.mockito.MockSettings;
import org.mockito.exceptions.verification.TooFewActualInvocations;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
public class MockitoMockUnitTest {
private static class CustomAnswer implements Answer<Boolean> {
@Override
public Boolean answer(InvocationOnMock invocation) throws Throwable {
return false;
}
}
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void whenUsingSimpleMock_thenCorrect() {
MyList listMock = mock(MyList.class);
when(listMock.add(anyString())).thenReturn(false);
boolean added = listMock.add(randomAlphabetic(6));
verify(listMock).add(anyString());
assertThat(added, is(false));
}
@Test
public void whenUsingMockWithName_thenCorrect() {
MyList listMock = mock(MyList.class, "myMock");
when(listMock.add(anyString())).thenReturn(false);
listMock.add(randomAlphabetic(6));
thrown.expect(TooFewActualInvocations.class);
thrown.expectMessage(containsString("myMock.add"));
verify(listMock, times(2)).add(anyString());
}
@Test
public void whenUsingMockWithAnswer_thenCorrect() {
MyList listMock = mock(MyList.class, new CustomAnswer());
boolean added = listMock.add(randomAlphabetic(6));
verify(listMock).add(anyString());
assertThat(added, is(false));
}
@Test
public void whenUsingMockWithSettings_thenCorrect() {
MockSettings customSettings = withSettings().defaultAnswer(new CustomAnswer());
MyList listMock = mock(MyList.class, customSettings);
boolean added = listMock.add(randomAlphabetic(6));
verify(listMock).add(anyString());
assertThat(added, is(false));
}
}
@@ -1,116 +0,0 @@
package com.baeldung.mockito;
import com.google.common.collect.Lists;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.InOrder;
import org.mockito.Mockito;
import org.mockito.exceptions.verification.NoInteractionsWanted;
import java.util.List;
import static org.hamcrest.Matchers.hasItem;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.*;
public class MockitoVerifyExamplesIntegrationTest {
// tests
@Test
public final void givenInteractionWithMockOccurred_whenVerifyingInteraction_thenCorrect() {
final List<String> mockedList = mock(MyList.class);
mockedList.size();
verify(mockedList).size();
}
@Test
public final void givenOneInteractionWithMockOccurred_whenVerifyingNumberOfInteractions_thenCorrect() {
final List<String> mockedList = mock(MyList.class);
mockedList.size();
verify(mockedList, times(1)).size();
}
@Test
public final void givenNoInteractionWithMockOccurred_whenVerifyingInteractions_thenCorrect() {
final List<String> mockedList = mock(MyList.class);
verifyNoInteractions(mockedList);
}
@Test
public final void givenNoInteractionWithMethodOfMockOccurred_whenVerifyingInteractions_thenCorrect() {
final List<String> mockedList = mock(MyList.class);
verify(mockedList, times(0)).size();
}
@Test(expected = NoInteractionsWanted.class)
public final void givenUnverifiedInteraction_whenVerifyingNoUnexpectedInteractions_thenFail() {
final List<String> mockedList = mock(MyList.class);
mockedList.size();
mockedList.clear();
verify(mockedList).size();
verifyNoMoreInteractions(mockedList);
}
@Test
public final void whenVerifyingOrderOfInteractions_thenCorrect() {
final List<String> 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<String> mockedList = mock(MyList.class);
mockedList.size();
verify(mockedList, never()).clear();
}
@Test
public final void whenVerifyingAnInteractionHasOccurredAtLeastOnce_thenCorrect() {
final List<String> mockedList = mock(MyList.class);
mockedList.clear();
mockedList.clear();
mockedList.clear();
verify(mockedList, atLeast(1)).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"));
}
}
@@ -1,117 +0,0 @@
package com.baeldung.mockito;
import com.google.common.collect.Lists;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.InOrder;
import org.mockito.Mockito;
import org.mockito.exceptions.verification.NoInteractionsWanted;
import java.util.List;
import static org.hamcrest.Matchers.hasItem;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.*;
public class MockitoVerifyExamplesUnitTest {
// tests
@Test
public final void givenInteractionWithMockOccurred_whenVerifyingInteraction_thenCorrect() {
final List<String> mockedList = mock(MyList.class);
mockedList.size();
verify(mockedList).size();
}
@Test
public final void givenOneInteractionWithMockOccurred_whenVerifyingNumberOfInteractions_thenCorrect() {
final List<String> mockedList = mock(MyList.class);
mockedList.size();
verify(mockedList, times(1)).size();
}
@Test
public final void givenNoInteractionWithMockOccurred_whenVerifyingInteractions_thenCorrect() {
final List<String> mockedList = mock(MyList.class);
verifyNoInteractions(mockedList);
}
@Test
public final void givenNoInteractionWithMethodOfMockOccurred_whenVerifyingInteractions_thenCorrect() {
final List<String> mockedList = mock(MyList.class);
verify(mockedList, times(0)).size();
}
@Test(expected = NoInteractionsWanted.class)
public final void givenUnverifiedInteraction_whenVerifyingNoUnexpectedInteractions_thenFail() {
final List<String> mockedList = mock(MyList.class);
mockedList.size();
mockedList.clear();
verify(mockedList).size();
verifyNoMoreInteractions(mockedList);
}
@Test
public final void whenVerifyingOrderOfInteractions_thenCorrect() {
final List<String> 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<String> mockedList = mock(MyList.class);
mockedList.size();
verify(mockedList, never()).clear();
}
@Test
public final void whenVerifyingAnInteractionHasOccurredAtLeastOnce_thenCorrect() {
final List<String> mockedList = mock(MyList.class);
mockedList.clear();
mockedList.clear();
mockedList.clear();
verify(mockedList, atLeast(1)).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"));
}
}
@@ -1,25 +0,0 @@
package com.baeldung.mockito;
import java.util.HashMap;
import java.util.Map;
class MyDictionary {
private Map<String, String> wordMap;
MyDictionary() {
wordMap = new HashMap<>();
}
MyDictionary(Map<String, String> wordMap) {
this.wordMap = wordMap;
}
public void add(final String word, final String meaning) {
wordMap.put(word, meaning);
}
String getMeaning(final String word) {
return wordMap.get(word);
}
}
@@ -1,25 +0,0 @@
package com.baeldung.mockito;
import java.util.AbstractList;
public class MyList extends AbstractList<String> {
@Override
public String get(final int index) {
return null;
}
@Override
public int size() {
return 1;
}
@Override
public void add(int index, String element) {
// no-op
}
final public int finalMethod() {
return 0;
}
}
@@ -1,66 +0,0 @@
package com.baeldung.mockito.voidmethods;
import com.baeldung.mockito.MyList;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.stubbing.Answer;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
@RunWith(MockitoJUnitRunner.class)
public class MockitoVoidMethodsUnitTest {
@Test
public void whenAddCalledVerified() {
MyList myList = mock(MyList.class);
myList.add(0, "");
verify(myList, times(1)).add(0, "");
}
@Test(expected = Exception.class)
public void givenNull_addThrows() {
MyList myList = mock(MyList.class);
doThrow().when(myList).add(isA(Integer.class), isNull());
myList.add(0, null);
}
@Test
public void whenAddCalledValueCaptured() {
MyList myList = mock(MyList.class);
ArgumentCaptor<String> valueCapture = ArgumentCaptor.forClass(String.class);
doNothing().when(myList).add(any(Integer.class), valueCapture.capture());
myList.add(0, "captured");
assertEquals("captured", valueCapture.getValue());
}
@Test
public void whenAddCalledAnswered() {
MyList myList = mock(MyList.class);
doAnswer(invocation -> {
Object arg0 = invocation.getArgument(0);
Object arg1 = invocation.getArgument(1);
//do something with the arguments here
assertEquals(3, arg0);
assertEquals("answer me", arg1);
return null;
}).when(myList).add(any(Integer.class), any(String.class));
myList.add(3, "answer me");
}
@Test
public void whenAddCalledRealMethodCalled() {
MyList myList = mock(MyList.class);
doCallRealMethod().when(myList).add(any(Integer.class), any(String.class));
myList.add(1, "real");
verify(myList, times(1)).add(1, "real");
}
}