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; package com.baeldung.mockito;
import org.junit.Test; import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertNotEquals;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import com.baeldung.mockito.MyList; import org.junit.Test;
public class MockFinalsUnitTest { public class MockFinalsUnitTest {
@Test @Test
public void whenMockFinalClassMockWorks() { 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 finalList = new FinalList();
FinalList mock = mock(FinalList.class); FinalList mock = mock(FinalList.class);
when(mock.size()).thenReturn(2); when(mock.size()).thenReturn(2);
assertNotEquals(mock.size(), finalList.size()); assertThat(mock.size()).isNotEqualTo(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());
} }
}
}

View File

@ -1,12 +1,18 @@
package com.baeldung.mockito.argumentcaptor; 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.Test;
import org.junit.runner.RunWith; 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 org.mockito.junit.MockitoJUnitRunner;
import static org.junit.Assert.*;
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
public class EmailServiceUnitTest { public class EmailServiceUnitTest {
@ -32,7 +38,7 @@ public class EmailServiceUnitTest {
Mockito.verify(platform).deliver(emailCaptor.capture()); Mockito.verify(platform).deliver(emailCaptor.capture());
Email emailCaptorValue = emailCaptor.getValue(); Email emailCaptorValue = emailCaptor.getValue();
assertEquals(Format.TEXT_ONLY, emailCaptorValue.getFormat()); assertThat(emailCaptorValue.getFormat()).isEqualTo(Format.TEXT_ONLY);
} }
@Test @Test
@ -45,7 +51,7 @@ public class EmailServiceUnitTest {
Mockito.verify(platform).deliver(emailCaptor.capture()); Mockito.verify(platform).deliver(emailCaptor.capture());
Email value = emailCaptor.getValue(); Email value = emailCaptor.getValue();
assertEquals(Format.HTML, value.getFormat()); assertThat(value.getFormat()).isEqualTo(Format.HTML);
} }
@Test @Test
@ -54,7 +60,7 @@ public class EmailServiceUnitTest {
ServiceStatus serviceStatus = emailService.checkServiceStatus(); ServiceStatus serviceStatus = emailService.checkServiceStatus();
assertEquals(ServiceStatus.UP, serviceStatus); assertThat(serviceStatus).isEqualTo(ServiceStatus.UP);
} }
@Test @Test
@ -63,7 +69,7 @@ public class EmailServiceUnitTest {
ServiceStatus serviceStatus = emailService.checkServiceStatus(); ServiceStatus serviceStatus = emailService.checkServiceStatus();
assertEquals(ServiceStatus.DOWN, serviceStatus); assertThat(serviceStatus).isEqualTo(ServiceStatus.DOWN);
} }
@Test @Test
@ -80,7 +86,7 @@ public class EmailServiceUnitTest {
Mockito.when(platform.authenticate(credentialsCaptor.capture())).thenReturn(AuthenticationStatus.AUTHENTICATED); Mockito.when(platform.authenticate(credentialsCaptor.capture())).thenReturn(AuthenticationStatus.AUTHENTICATED);
assertTrue(emailService.authenticatedSuccessfully(credentials)); assertTrue(emailService.authenticatedSuccessfully(credentials));
assertEquals(credentials, credentialsCaptor.getValue()); assertThat(credentialsCaptor.getValue()).isEqualTo(credentials);
} }
@Test @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; 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.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.mockito.Spy; import org.mockito.Spy;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.MockitoJUnitRunner;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.assertEquals;
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
public class MockitoSpyUnitTest { public class MockitoSpyUnitTest {
@Spy
private List<String> aSpyList = new ArrayList<String>();
@Test @Test
public void whenSpyingOnList_thenCorrect() { public void whenSpyingOnList_thenCorrect() {
final List<String> list = new ArrayList<String>(); final List<String> list = new ArrayList<String>();
@ -28,9 +26,12 @@ public class MockitoSpyUnitTest {
Mockito.verify(spyList).add("one"); Mockito.verify(spyList).add("one");
Mockito.verify(spyList).add("two"); Mockito.verify(spyList).add("two");
assertEquals(2, spyList.size()); assertThat(spyList).hasSize(2);
} }
@Spy
private List<String> aSpyList = new ArrayList<String>();
@Test @Test
public void whenUsingTheSpyAnnotation_thenObjectIsSpied() { public void whenUsingTheSpyAnnotation_thenObjectIsSpied() {
aSpyList.add("one"); aSpyList.add("one");
@ -39,7 +40,7 @@ public class MockitoSpyUnitTest {
Mockito.verify(aSpyList).add("one"); Mockito.verify(aSpyList).add("one");
Mockito.verify(aSpyList).add("two"); Mockito.verify(aSpyList).add("two");
assertEquals(2, aSpyList.size()); assertThat(aSpyList).hasSize(2);
} }
@Test @Test
@ -50,7 +51,7 @@ public class MockitoSpyUnitTest {
assertEquals(0, spyList.size()); assertEquals(0, spyList.size());
Mockito.doReturn(100).when(spyList).size(); Mockito.doReturn(100).when(spyList).size();
assertEquals(100, spyList.size()); assertThat(spyList).hasSize(100);
} }
@Test @Test
@ -60,17 +61,17 @@ public class MockitoSpyUnitTest {
mockedList.add("one"); mockedList.add("one");
Mockito.verify(mockedList).add("one"); Mockito.verify(mockedList).add("one");
assertEquals(0, mockedList.size()); assertThat(mockedList).hasSize(0);
} }
@Test @Test
public void whenCreateSpy_thenCreate() { public void whenCreateSpy_thenCreate() {
final List<String> spyList = Mockito.spy(new ArrayList<String>()); final List<String> spyList = Mockito.spy(new ArrayList<>());
spyList.add("one"); spyList.add("one");
Mockito.verify(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; 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.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor; import org.mockito.ArgumentCaptor;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.stubbing.Answer;
import static org.junit.Assert.assertEquals; import com.baeldung.mockito.MyList;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
public class MockitoVoidMethodsUnitTest { public class MockitoVoidMethodsUnitTest {