JAVA-12032 Move Mockito ebook articles code to common module - mockito-simple- Delete mockito-3 since it had only 1 article which was moved to mockito-simple
This commit is contained in:
-7
@@ -1,7 +0,0 @@
|
||||
package com.baeldung.mockito.argumentcaptor;
|
||||
|
||||
public enum AuthenticationStatus {
|
||||
AUTHENTICATED,
|
||||
NOT_AUTHENTICATED,
|
||||
ERROR
|
||||
}
|
||||
-14
@@ -1,14 +0,0 @@
|
||||
package com.baeldung.mockito.argumentcaptor;
|
||||
|
||||
public class Credentials {
|
||||
private final String name;
|
||||
private final String password;
|
||||
private final String key;
|
||||
|
||||
public Credentials(String name, String password, String key) {
|
||||
this.name = name;
|
||||
this.password = password;
|
||||
this.key = key;
|
||||
}
|
||||
}
|
||||
|
||||
-10
@@ -1,10 +0,0 @@
|
||||
package com.baeldung.mockito.argumentcaptor;
|
||||
|
||||
public interface DeliveryPlatform {
|
||||
|
||||
void deliver(Email email);
|
||||
|
||||
String getServiceStatus();
|
||||
|
||||
AuthenticationStatus authenticate(Credentials credentials);
|
||||
}
|
||||
-47
@@ -1,47 +0,0 @@
|
||||
package com.baeldung.mockito.argumentcaptor;
|
||||
|
||||
public class Email {
|
||||
|
||||
private String address;
|
||||
private String subject;
|
||||
private String body;
|
||||
private Format format;
|
||||
|
||||
public Email(String address, String subject, String body) {
|
||||
this.address = address;
|
||||
this.subject = subject;
|
||||
this.body = body;
|
||||
}
|
||||
|
||||
public String getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void setAddress(String address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
public String getSubject() {
|
||||
return subject;
|
||||
}
|
||||
|
||||
public void setSubject(String subject) {
|
||||
this.subject = subject;
|
||||
}
|
||||
|
||||
public String getBody() {
|
||||
return body;
|
||||
}
|
||||
|
||||
public void setBody(String body) {
|
||||
this.body = body;
|
||||
}
|
||||
|
||||
public Format getFormat() {
|
||||
return format;
|
||||
}
|
||||
|
||||
public void setFormat(Format format) {
|
||||
this.format = format;
|
||||
}
|
||||
}
|
||||
-36
@@ -1,36 +0,0 @@
|
||||
package com.baeldung.mockito.argumentcaptor;
|
||||
|
||||
public class EmailService {
|
||||
|
||||
private DeliveryPlatform platform;
|
||||
|
||||
public EmailService(DeliveryPlatform platform) {
|
||||
this.platform = platform;
|
||||
}
|
||||
|
||||
public void send(String to, String subject, String body, boolean html) {
|
||||
Format format = Format.TEXT_ONLY;
|
||||
if (html) {
|
||||
format = Format.HTML;
|
||||
}
|
||||
Email email = new Email(to, subject, body);
|
||||
email.setFormat(format);
|
||||
platform.deliver(email);
|
||||
}
|
||||
|
||||
public ServiceStatus checkServiceStatus() {
|
||||
if (platform.getServiceStatus().equals("OK")) {
|
||||
return ServiceStatus.UP;
|
||||
} else {
|
||||
return ServiceStatus.DOWN;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean authenticatedSuccessfully(Credentials credentials) {
|
||||
if (platform.authenticate(credentials).equals(AuthenticationStatus.AUTHENTICATED)) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
-6
@@ -1,6 +0,0 @@
|
||||
package com.baeldung.mockito.argumentcaptor;
|
||||
|
||||
public enum Format {
|
||||
TEXT_ONLY,
|
||||
HTML
|
||||
}
|
||||
-7
@@ -1,7 +0,0 @@
|
||||
package com.baeldung.mockito.argumentcaptor;
|
||||
|
||||
public enum ServiceStatus {
|
||||
UP,
|
||||
DOWN,
|
||||
AUTHENTICATED
|
||||
}
|
||||
-93
@@ -1,93 +0,0 @@
|
||||
package com.baeldung.mockito.argumentcaptor;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.*;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class EmailServiceUnitTest {
|
||||
|
||||
@Mock
|
||||
DeliveryPlatform platform;
|
||||
|
||||
@InjectMocks
|
||||
EmailService emailService;
|
||||
|
||||
@Captor
|
||||
ArgumentCaptor<Email> emailCaptor;
|
||||
|
||||
@Captor
|
||||
ArgumentCaptor<Credentials> credentialsCaptor;
|
||||
|
||||
@Test
|
||||
public void whenDoesNotSupportHtml_expectTextOnlyEmailFormat() {
|
||||
String to = "info@baeldung.com";
|
||||
String subject = "Using ArgumentCaptor";
|
||||
String body = "Hey, let'use ArgumentCaptor";
|
||||
|
||||
emailService.send(to, subject, body, false);
|
||||
|
||||
Mockito.verify(platform).deliver(emailCaptor.capture());
|
||||
Email emailCaptorValue = emailCaptor.getValue();
|
||||
assertEquals(Format.TEXT_ONLY, emailCaptorValue.getFormat());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDoesSupportHtml_expectHTMLEmailFormat() {
|
||||
String to = "info@baeldung.com";
|
||||
String subject = "Using ArgumentCaptor";
|
||||
String body = "<html><body>Hey, let'use ArgumentCaptor</body></html>";
|
||||
|
||||
emailService.send(to, subject, body, true);
|
||||
|
||||
Mockito.verify(platform).deliver(emailCaptor.capture());
|
||||
Email value = emailCaptor.getValue();
|
||||
assertEquals(Format.HTML, value.getFormat());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenServiceRunning_expectUpResponse() {
|
||||
Mockito.when(platform.getServiceStatus()).thenReturn("OK");
|
||||
|
||||
ServiceStatus serviceStatus = emailService.checkServiceStatus();
|
||||
|
||||
assertEquals(ServiceStatus.UP, serviceStatus);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenServiceNotRunning_expectDownResponse() {
|
||||
Mockito.when(platform.getServiceStatus()).thenReturn("Error");
|
||||
|
||||
ServiceStatus serviceStatus = emailService.checkServiceStatus();
|
||||
|
||||
assertEquals(ServiceStatus.DOWN, serviceStatus);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingArgumentMatcherForValidCredentials_expectTrue() {
|
||||
Credentials credentials = new Credentials("baeldung", "correct_password", "correct_key");
|
||||
Mockito.when(platform.authenticate(Mockito.eq(credentials))).thenReturn(AuthenticationStatus.AUTHENTICATED);
|
||||
|
||||
assertTrue(emailService.authenticatedSuccessfully(credentials));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingArgumentCaptorForValidCredentials_expectTrue() {
|
||||
Credentials credentials = new Credentials("baeldung", "correct_password", "correct_key");
|
||||
Mockito.when(platform.authenticate(credentialsCaptor.capture())).thenReturn(AuthenticationStatus.AUTHENTICATED);
|
||||
|
||||
assertTrue(emailService.authenticatedSuccessfully(credentials));
|
||||
assertEquals(credentials, credentialsCaptor.getValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNotAuthenticated_expectFalse() {
|
||||
Credentials credentials = new Credentials("baeldung", "incorrect_password", "incorrect_key");
|
||||
Mockito.when(platform.authenticate(Mockito.eq(credentials))).thenReturn(AuthenticationStatus.NOT_AUTHENTICATED);
|
||||
|
||||
assertFalse(emailService.authenticatedSuccessfully(credentials));
|
||||
}
|
||||
}
|
||||
-38
@@ -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!"));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
-76
@@ -1,76 +0,0 @@
|
||||
package com.baeldung.mockito.spy;
|
||||
|
||||
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>();
|
||||
final List<String> spyList = Mockito.spy(list);
|
||||
|
||||
spyList.add("one");
|
||||
spyList.add("two");
|
||||
|
||||
Mockito.verify(spyList).add("one");
|
||||
Mockito.verify(spyList).add("two");
|
||||
|
||||
assertEquals(2, spyList.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingTheSpyAnnotation_thenObjectIsSpied() {
|
||||
aSpyList.add("one");
|
||||
aSpyList.add("two");
|
||||
|
||||
Mockito.verify(aSpyList).add("one");
|
||||
Mockito.verify(aSpyList).add("two");
|
||||
|
||||
assertEquals(2, aSpyList.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenStubASpy_thenStubbed() {
|
||||
final List<String> list = new ArrayList<String>();
|
||||
final List<String> spyList = Mockito.spy(list);
|
||||
|
||||
assertEquals(0, spyList.size());
|
||||
|
||||
Mockito.doReturn(100).when(spyList).size();
|
||||
assertEquals(100, spyList.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCreateMock_thenCreated() {
|
||||
final List<String> mockedList = Mockito.mock(ArrayList.class);
|
||||
|
||||
mockedList.add("one");
|
||||
Mockito.verify(mockedList).add("one");
|
||||
|
||||
assertEquals(0, mockedList.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCreateSpy_thenCreate() {
|
||||
final List<String> spyList = Mockito.spy(new ArrayList<String>());
|
||||
|
||||
spyList.add("one");
|
||||
Mockito.verify(spyList).add("one");
|
||||
|
||||
assertEquals(1, spyList.size());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user