Merge pull request #12585 from dkapil/task/JAVA-13630

Task/java 13630
This commit is contained in:
Loredana Crusoveanu 2022-08-11 15:16:16 +03:00 committed by GitHub
commit 3252ba5775
6 changed files with 89 additions and 87 deletions

View File

@ -1,37 +1,32 @@
package com.baeldung.mockito;
import org.junit.Test;
import static org.junit.Assert.assertNotEquals;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import com.baeldung.mockito.MyList;
import org.junit.Test;
public class MockFinalsUnitTest {
@Test
public void whenMockFinalClassMockWorks() {
@Test
public void whenMockFinalMethodMockWorks() {
MyList myList = new MyList();
MyList mock = mock(MyList.class);
when(mock.finalMethod()).thenReturn(1);
assertThat(mock.finalMethod()).isNotEqualTo(myList.finalMethod());
}
@Test
public void whenMockFinalClassMockWorks() {
FinalList finalList = new FinalList();
FinalList mock = mock(FinalList.class);
when(mock.size()).thenReturn(2);
assertNotEquals(mock.size(), finalList.size());
}
@Test
public void whenMockFinalMethodMockWorks() {
MyList myList = new MyList();
MyList mock = mock(MyList.class);
when(mock.finalMethod()).thenReturn(1);
assertNotEquals(mock.finalMethod(), myList.finalMethod());
assertThat(mock.size()).isNotEqualTo(finalList.size());
}
}
}

View File

@ -1,12 +1,18 @@
package com.baeldung.mockito.argumentcaptor;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.*;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import static org.junit.Assert.*;
@RunWith(MockitoJUnitRunner.class)
public class EmailServiceUnitTest {
@ -32,7 +38,7 @@ public class EmailServiceUnitTest {
Mockito.verify(platform).deliver(emailCaptor.capture());
Email emailCaptorValue = emailCaptor.getValue();
assertEquals(Format.TEXT_ONLY, emailCaptorValue.getFormat());
assertThat(emailCaptorValue.getFormat()).isEqualTo(Format.TEXT_ONLY);
}
@Test
@ -45,7 +51,7 @@ public class EmailServiceUnitTest {
Mockito.verify(platform).deliver(emailCaptor.capture());
Email value = emailCaptor.getValue();
assertEquals(Format.HTML, value.getFormat());
assertThat(value.getFormat()).isEqualTo(Format.HTML);
}
@Test
@ -54,7 +60,7 @@ public class EmailServiceUnitTest {
ServiceStatus serviceStatus = emailService.checkServiceStatus();
assertEquals(ServiceStatus.UP, serviceStatus);
assertThat(serviceStatus).isEqualTo(ServiceStatus.UP);
}
@Test
@ -63,7 +69,7 @@ public class EmailServiceUnitTest {
ServiceStatus serviceStatus = emailService.checkServiceStatus();
assertEquals(ServiceStatus.DOWN, serviceStatus);
assertThat(serviceStatus).isEqualTo(ServiceStatus.DOWN);
}
@Test
@ -80,7 +86,7 @@ public class EmailServiceUnitTest {
Mockito.when(platform.authenticate(credentialsCaptor.capture())).thenReturn(AuthenticationStatus.AUTHENTICATED);
assertTrue(emailService.authenticatedSuccessfully(credentials));
assertEquals(credentials, credentialsCaptor.getValue());
assertThat(credentialsCaptor.getValue()).isEqualTo(credentials);
}
@Test

View File

@ -0,0 +1,30 @@
package com.baeldung.mockito.spy;
import static org.assertj.core.api.Assertions.assertThatNoException;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import org.mockito.Mockito;
import org.mockito.exceptions.misusing.NotAMockException;
public class MockitoMisusingMockOrSpyUnitTest {
@Test
public void givenNotASpy_whenDoReturn_thenThrowNotAMock() {
List<String> list = new ArrayList<String>();
assertThatThrownBy(() -> Mockito.doReturn(100).when(list).size())
.isInstanceOf(NotAMockException.class)
.hasMessageContaining("Argument passed to when() is not a mock!");
}
@Test
public void givenASpy_whenDoReturn_thenNoError() {
final List<String> spyList = Mockito.spy(new ArrayList<>());
assertThatNoException().isThrownBy(() -> Mockito.doReturn(100).when(spyList).size());
}
}

View File

@ -1,38 +0,0 @@
package com.baeldung.mockito.spy;
import org.junit.After;
import org.junit.Test;
import org.mockito.Mockito;
import org.mockito.exceptions.misusing.NotAMockException;
import org.mockito.internal.progress.ThreadSafeMockingProgress;
import java.util.ArrayList;
import java.util.List;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertThat;
import static org.junit.jupiter.api.Assertions.fail;
public class MockitoMisusingUnitTest {
@After
public void tearDown() {
ThreadSafeMockingProgress.mockingProgress().reset();
}
@Test
public void givenNotASpy_whenDoReturn_thenThrowNotAMock() {
try {
List<String> list = new ArrayList<String>();
Mockito.doReturn(100, Mockito.withSettings().lenient())
.when(list)
.size();
fail("Should have thrown a NotAMockException because 'list' is not a mock!");
} catch (NotAMockException e) {
assertThat(e.getMessage(), containsString("Argument passed to when() is not a mock!"));
}
}
}

View File

@ -1,22 +1,20 @@
package com.baeldung.mockito.spy;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.assertEquals;
@RunWith(MockitoJUnitRunner.class)
public class MockitoSpyUnitTest {
@Spy
private List<String> aSpyList = new ArrayList<String>();
@Test
public void whenSpyingOnList_thenCorrect() {
final List<String> list = new ArrayList<String>();
@ -28,9 +26,12 @@ public class MockitoSpyUnitTest {
Mockito.verify(spyList).add("one");
Mockito.verify(spyList).add("two");
assertEquals(2, spyList.size());
assertThat(spyList).hasSize(2);
}
@Spy
private List<String> aSpyList = new ArrayList<String>();
@Test
public void whenUsingTheSpyAnnotation_thenObjectIsSpied() {
aSpyList.add("one");
@ -39,7 +40,7 @@ public class MockitoSpyUnitTest {
Mockito.verify(aSpyList).add("one");
Mockito.verify(aSpyList).add("two");
assertEquals(2, aSpyList.size());
assertThat(aSpyList).hasSize(2);
}
@Test
@ -50,7 +51,7 @@ public class MockitoSpyUnitTest {
assertEquals(0, spyList.size());
Mockito.doReturn(100).when(spyList).size();
assertEquals(100, spyList.size());
assertThat(spyList).hasSize(100);
}
@Test
@ -60,17 +61,17 @@ public class MockitoSpyUnitTest {
mockedList.add("one");
Mockito.verify(mockedList).add("one");
assertEquals(0, mockedList.size());
assertThat(mockedList).hasSize(0);
}
@Test
public void whenCreateSpy_thenCreate() {
final List<String> spyList = Mockito.spy(new ArrayList<String>());
final List<String> spyList = Mockito.spy(new ArrayList<>());
spyList.add("one");
Mockito.verify(spyList).add("one");
assertEquals(1, spyList.size());
assertThat(spyList).hasSize(1);
}
}

View File

@ -1,15 +1,23 @@
package com.baeldung.mockito.voidmethods;
import com.baeldung.mockito.MyList;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.isA;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doCallRealMethod;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.stubbing.Answer;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import com.baeldung.mockito.MyList;
@RunWith(MockitoJUnitRunner.class)
public class MockitoVoidMethodsUnitTest {