JAVA-13627 Removed MockitoMockIntegrationTest as it was a duplicate, other cleanup and API upgrades
This commit is contained in:
parent
4dab3eb46d
commit
4e13672a93
|
@ -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));
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue