JAVA-13630 Updated code for Mockito - Using Spies Article

This commit is contained in:
Dhawal Kapil 2022-07-31 23:23:37 +05:30
parent 17d2d5a999
commit 57aa81d552
3 changed files with 45 additions and 52 deletions

View File

@ -0,0 +1,30 @@
package com.baeldung.mockito.spy;
import static org.assertj.core.api.Assertions.assertThatNoException;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import org.mockito.Mockito;
import org.mockito.exceptions.misusing.NotAMockException;
public class MockitoMisusingMockOrSpyUnitTest {
@Test
public void givenNotASpy_whenDoReturn_thenThrowNotAMock() {
List<String> list = new ArrayList<String>();
assertThatThrownBy(() -> Mockito.doReturn(100).when(list).size())
.isInstanceOf(NotAMockException.class)
.hasMessageContaining("Argument passed to when() is not a mock!");
}
@Test
public void givenASpy_whenDoReturn_thenNoError() {
final List<String> spyList = Mockito.spy(new ArrayList<>());
assertThatNoException().isThrownBy(() -> Mockito.doReturn(100).when(spyList).size());
}
}

View File

@ -1,38 +0,0 @@
package com.baeldung.mockito.spy;
import org.junit.After;
import org.junit.Test;
import org.mockito.Mockito;
import org.mockito.exceptions.misusing.NotAMockException;
import org.mockito.internal.progress.ThreadSafeMockingProgress;
import java.util.ArrayList;
import java.util.List;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertThat;
import static org.junit.jupiter.api.Assertions.fail;
public class MockitoMisusingUnitTest {
@After
public void tearDown() {
ThreadSafeMockingProgress.mockingProgress().reset();
}
@Test
public void givenNotASpy_whenDoReturn_thenThrowNotAMock() {
try {
List<String> list = new ArrayList<String>();
Mockito.doReturn(100, Mockito.withSettings().lenient())
.when(list)
.size();
fail("Should have thrown a NotAMockException because 'list' is not a mock!");
} catch (NotAMockException e) {
assertThat(e.getMessage(), containsString("Argument passed to when() is not a mock!"));
}
}
}

View File

@ -1,22 +1,20 @@
package com.baeldung.mockito.spy;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.assertEquals;
@RunWith(MockitoJUnitRunner.class)
public class MockitoSpyUnitTest {
@Spy
private List<String> aSpyList = new ArrayList<String>();
@Test
public void whenSpyingOnList_thenCorrect() {
final List<String> list = new ArrayList<String>();
@ -28,9 +26,12 @@ public class MockitoSpyUnitTest {
Mockito.verify(spyList).add("one");
Mockito.verify(spyList).add("two");
assertEquals(2, spyList.size());
assertThat(spyList).hasSize(2);
}
@Spy
private List<String> aSpyList = new ArrayList<String>();
@Test
public void whenUsingTheSpyAnnotation_thenObjectIsSpied() {
aSpyList.add("one");
@ -39,7 +40,7 @@ public class MockitoSpyUnitTest {
Mockito.verify(aSpyList).add("one");
Mockito.verify(aSpyList).add("two");
assertEquals(2, aSpyList.size());
assertThat(aSpyList).hasSize(2);
}
@Test
@ -50,7 +51,7 @@ public class MockitoSpyUnitTest {
assertEquals(0, spyList.size());
Mockito.doReturn(100).when(spyList).size();
assertEquals(100, spyList.size());
assertThat(spyList).hasSize(100);
}
@Test
@ -60,17 +61,17 @@ public class MockitoSpyUnitTest {
mockedList.add("one");
Mockito.verify(mockedList).add("one");
assertEquals(0, mockedList.size());
assertThat(mockedList).hasSize(0);
}
@Test
public void whenCreateSpy_thenCreate() {
final List<String> spyList = Mockito.spy(new ArrayList<String>());
final List<String> spyList = Mockito.spy(new ArrayList<>());
spyList.add("one");
Mockito.verify(spyList).add("one");
assertEquals(1, spyList.size());
assertThat(spyList).hasSize(1);
}
}