JAVA-13627 Updated code to use assertThatThrownBy instead of assertThrows

This commit is contained in:
Dhawal Kapil 2022-07-31 21:57:45 +05:30
parent 8db0d7b277
commit 53629eeb0b
1 changed files with 5 additions and 6 deletions

View File

@ -2,7 +2,7 @@ package com.baeldung.mockito;
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
@ -33,11 +33,10 @@ public class MockitoMockUnitTest {
MyList listMock = mock(MyList.class, "myMock"); MyList listMock = mock(MyList.class, "myMock");
when(listMock.add(anyString())).thenReturn(false); when(listMock.add(anyString())).thenReturn(false);
listMock.add(randomAlphabetic(6)); listMock.add(randomAlphabetic(6));
Throwable exceptionThrown = assertThrows(TooFewActualInvocations.class, assertThatThrownBy(() -> verify(listMock, times(2)).add(anyString()))
() -> verify(listMock, times(2)).add(anyString())); .isInstanceOf(TooFewActualInvocations.class)
.hasMessageContaining("myMock.add");
assertThat(exceptionThrown.getMessage()).contains("myMock.add");
} }
private static class CustomAnswer implements Answer<Boolean> { private static class CustomAnswer implements Answer<Boolean> {