Merge pull request #12534 from dkapil/task/JAVA-12033

JAVA-12033 Upgraded assertion libraries and minor cleanup
This commit is contained in:
kwoyke 2022-07-27 09:21:20 +02:00 committed by GitHub
commit d378b72d3a
1 changed files with 36 additions and 38 deletions

View File

@ -1,19 +1,18 @@
package com.baeldung.mockito; 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.junit.Test;
import org.mockito.Mockito; import org.mockito.Mockito;
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; public class MockitoWhenThenExamplesUnitTest {
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 @Test
public final void whenMockReturnBehaviorIsConfigured_thenBehaviorIsVerified() { public final void whenMockReturnBehaviorIsConfigured_thenBehaviorIsVerified() {
@ -21,7 +20,7 @@ public class MockitoConfigExamplesUnitTest {
when(listMock.add(anyString())).thenReturn(false); when(listMock.add(anyString())).thenReturn(false);
final boolean added = listMock.add(randomAlphabetic(6)); final boolean added = listMock.add(randomAlphabetic(6));
assertThat(added, is(false)); assertThat(added).isFalse();
} }
@Test @Test
@ -30,7 +29,7 @@ public class MockitoConfigExamplesUnitTest {
doReturn(false).when(listMock).add(anyString()); doReturn(false).when(listMock).add(anyString());
final boolean added = listMock.add(randomAlphabetic(6)); final boolean added = listMock.add(randomAlphabetic(6));
assertThat(added, is(false)); assertThat(added).isFalse();
} }
@Test(expected = IllegalStateException.class) @Test(expected = IllegalStateException.class)
@ -41,6 +40,14 @@ public class MockitoConfigExamplesUnitTest {
listMock.add(randomAlphabetic(6)); listMock.add(randomAlphabetic(6));
} }
@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 = NullPointerException.class) @Test(expected = NullPointerException.class)
public final void whenMethodHasNoReturnType_whenConfiguringBehaviorOfMethod_thenPossible() { public final void whenMethodHasNoReturnType_whenConfiguringBehaviorOfMethod_thenPossible() {
final MyList listMock = Mockito.mock(MyList.class); final MyList listMock = Mockito.mock(MyList.class);
@ -49,14 +56,6 @@ public class MockitoConfigExamplesUnitTest {
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) @Test(expected = IllegalStateException.class)
public final void givenBehaviorIsConfiguredToThrowExceptionOnSecondCall_whenCallingTwice_thenExceptionIsThrown() { public final void givenBehaviorIsConfiguredToThrowExceptionOnSecondCall_whenCallingTwice_thenExceptionIsThrown() {
final MyList listMock = Mockito.mock(MyList.class); final MyList listMock = Mockito.mock(MyList.class);
@ -66,23 +65,6 @@ public class MockitoConfigExamplesUnitTest {
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) @Test(expected = NullPointerException.class)
public final void givenSpy_whenConfiguringBehaviorOfSpy_thenCorrectlyConfigured() { public final void givenSpy_whenConfiguringBehaviorOfSpy_thenCorrectlyConfigured() {
final MyList instance = new MyList(); final MyList instance = new MyList();
@ -92,4 +74,20 @@ public class MockitoConfigExamplesUnitTest {
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");
}
} }