diff --git a/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/spy/MockitoMisusingMockOrSpyUnitTest.java b/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/spy/MockitoMisusingMockOrSpyUnitTest.java new file mode 100644 index 0000000000..b3f470427a --- /dev/null +++ b/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/spy/MockitoMisusingMockOrSpyUnitTest.java @@ -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 list = new ArrayList(); + + 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 spyList = Mockito.spy(new ArrayList<>()); + + assertThatNoException().isThrownBy(() -> Mockito.doReturn(100).when(spyList).size()); + } +} diff --git a/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/spy/MockitoMisusingUnitTest.java b/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/spy/MockitoMisusingUnitTest.java deleted file mode 100644 index ea12061fc6..0000000000 --- a/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/spy/MockitoMisusingUnitTest.java +++ /dev/null @@ -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 list = new ArrayList(); - - 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!")); - } - } - -} diff --git a/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/spy/MockitoSpyUnitTest.java b/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/spy/MockitoSpyUnitTest.java index d4696e482b..28ca0c327b 100644 --- a/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/spy/MockitoSpyUnitTest.java +++ b/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/spy/MockitoSpyUnitTest.java @@ -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 aSpyList = new ArrayList(); - @Test public void whenSpyingOnList_thenCorrect() { final List list = new ArrayList(); @@ -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 aSpyList = new ArrayList(); + @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 spyList = Mockito.spy(new ArrayList()); + final List spyList = Mockito.spy(new ArrayList<>()); spyList.add("one"); Mockito.verify(spyList).add("one"); - assertEquals(1, spyList.size()); + assertThat(spyList).hasSize(1); } }