From 4e13672a9369c6dc6835ee5cc4133877ae0a62ae Mon Sep 17 00:00:00 2001 From: Dhawal Kapil Date: Tue, 26 Jul 2022 18:23:50 +0530 Subject: [PATCH] JAVA-13627 Removed MockitoMockIntegrationTest as it was a duplicate, other cleanup and API upgrades --- .../mockito/MockitoMockIntegrationTest.java | 69 ------------------- .../baeldung/mockito/MockitoMockUnitTest.java | 46 ++++++------- 2 files changed, 22 insertions(+), 93 deletions(-) delete mode 100644 testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/MockitoMockIntegrationTest.java diff --git a/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/MockitoMockIntegrationTest.java b/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/MockitoMockIntegrationTest.java deleted file mode 100644 index 944d41316d..0000000000 --- a/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/MockitoMockIntegrationTest.java +++ /dev/null @@ -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 { - @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)); - } -} \ No newline at end of file diff --git a/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/MockitoMockUnitTest.java b/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/MockitoMockUnitTest.java index 27af3d8d1e..80f01338e6 100644 --- a/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/MockitoMockUnitTest.java +++ b/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/MockitoMockUnitTest.java @@ -1,15 +1,16 @@ 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 static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import static org.mockito.Mockito.withSettings; 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; @@ -17,16 +18,6 @@ import org.mockito.stubbing.Answer; public class MockitoMockUnitTest { - private static class CustomAnswer implements Answer { - @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); @@ -34,7 +25,7 @@ public class MockitoMockUnitTest { boolean added = listMock.add(randomAlphabetic(6)); verify(listMock).add(anyString()); - assertThat(added, is(false)); + assertThat(added).isFalse(); } @Test @@ -43,19 +34,26 @@ public class MockitoMockUnitTest { 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()); + Throwable exceptionThrown = assertThrows(TooFewActualInvocations.class, + () -> verify(listMock, times(2)).add(anyString())); + + assertThat(exceptionThrown.getMessage()).contains("myMock.add"); } + private static class CustomAnswer implements Answer { + @Override + public Boolean answer(InvocationOnMock invocation) throws Throwable { + return false; + } + } + @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)); + assertThat(added).isFalse(); } @Test @@ -65,6 +63,6 @@ public class MockitoMockUnitTest { boolean added = listMock.add(randomAlphabetic(6)); verify(listMock).add(anyString()); - assertThat(added, is(false)); + assertThat(added).isFalse(); } } \ No newline at end of file