From 4dab3eb46d82c34060ad4dbf77cdb2eb49ea2d11 Mon Sep 17 00:00:00 2001 From: Dhawal Kapil Date: Tue, 26 Jul 2022 12:58:41 +0530 Subject: [PATCH] JAVA-12033 Upgraded assertion libraries and minor cleanup --- ...a => MockitoWhenThenExamplesUnitTest.java} | 74 +++++++++---------- 1 file changed, 36 insertions(+), 38 deletions(-) rename testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/{MockitoConfigExamplesUnitTest.java => MockitoWhenThenExamplesUnitTest.java} (84%) diff --git a/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/MockitoConfigExamplesUnitTest.java b/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/MockitoWhenThenExamplesUnitTest.java similarity index 84% rename from testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/MockitoConfigExamplesUnitTest.java rename to testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/MockitoWhenThenExamplesUnitTest.java index 6a5f797b41..d110fff00f 100644 --- a/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/MockitoConfigExamplesUnitTest.java +++ b/testing-modules/mockito-simple/src/test/java/com/baeldung/mockito/MockitoWhenThenExamplesUnitTest.java @@ -1,19 +1,18 @@ package com.baeldung.mockito; +import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.when; + 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 +public class MockitoWhenThenExamplesUnitTest { @Test public final void whenMockReturnBehaviorIsConfigured_thenBehaviorIsVerified() { @@ -21,7 +20,7 @@ public class MockitoConfigExamplesUnitTest { when(listMock.add(anyString())).thenReturn(false); final boolean added = listMock.add(randomAlphabetic(6)); - assertThat(added, is(false)); + assertThat(added).isFalse(); } @Test @@ -30,7 +29,7 @@ public class MockitoConfigExamplesUnitTest { doReturn(false).when(listMock).add(anyString()); final boolean added = listMock.add(randomAlphabetic(6)); - assertThat(added, is(false)); + assertThat(added).isFalse(); } @Test(expected = IllegalStateException.class) @@ -41,14 +40,6 @@ public class MockitoConfigExamplesUnitTest { 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); @@ -56,6 +47,14 @@ public class MockitoConfigExamplesUnitTest { 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(expected = IllegalStateException.class) public final void givenBehaviorIsConfiguredToThrowExceptionOnSecondCall_whenCallingTwice_thenExceptionIsThrown() { @@ -66,23 +65,6 @@ public class MockitoConfigExamplesUnitTest { 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(); @@ -91,5 +73,21 @@ public class MockitoConfigExamplesUnitTest { doThrow(NullPointerException.class).when(spy).size(); spy.size(); } + + @Test + public final void whenMockMethodCallIsConfiguredToCallTheRealMethod_thenRealMethodIsCalled() { + final MyList listMock = Mockito.mock(MyList.class); + when(listMock.size()).thenCallRealMethod(); + assertThat(listMock).hasSize(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).isEqualTo("Always the same"); + } }