Merge pull request #12535 from dkapil/task/JAVA-13627

Task/java 13627
This commit is contained in:
kwoyke 2022-07-27 09:24:36 +02:00 committed by GitHub
commit fe6bab6975
2 changed files with 22 additions and 93 deletions

View File

@ -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<Boolean> {
@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));
}
}

View File

@ -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<Boolean> {
@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<Boolean> {
@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();
}
}