Initial commit of ArgumentCaptor

This commit is contained in:
developerDiv 2020-07-11 16:42:03 +01:00
parent 8e09ba0735
commit 2a88d24386
8 changed files with 228 additions and 0 deletions

View File

@ -0,0 +1,7 @@
package com.baeldung.mockito.argumentcaptor;
public enum AuthenticationStatus {
AUTHENTICATED,
NOT_AUTHENTICATED,
ERROR
}

View File

@ -0,0 +1,15 @@
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;
}
}

View File

@ -0,0 +1,10 @@
package com.baeldung.mockito.argumentcaptor;
public interface DeliveryPlatform {
void send(Email email);
String getServiceStatus();
AuthenticationStatus authenticate(Credentials credentials);
}

View File

@ -0,0 +1,47 @@
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;
}
}

View File

@ -0,0 +1,36 @@
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.send(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;
}
}
}

View File

@ -0,0 +1,6 @@
package com.baeldung.mockito.argumentcaptor;
public enum Format {
TEXT_ONLY,
HTML
}

View File

@ -0,0 +1,7 @@
package com.baeldung.mockito.argumentcaptor;
public enum ServiceStatus {
UP,
DOWN,
AUTHENTICATED
}

View File

@ -0,0 +1,100 @@
package com.baeldung.mockito.argumentcaptor;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class EmailServiceUnitTest {
@InjectMocks
EmailService emailService;
@Mock
DeliveryPlatform platform;
@Captor
ArgumentCaptor<Email> emailCaptor;
@Captor
ArgumentCaptor<Credentials> credentialsCaptor;
@Test
public void whenDoesNotSupportHtml_expectTextOnlyEmailFormat() {
String to = "info@baeldung.com";
String subject = "Using ArgumentCaptor";
String body = "Article on using ArgumentCaptor";
emailService.send(to, subject, body, false);
verify(platform).send(emailCaptor.capture());
Email emailCaptorValue = emailCaptor.getValue();
assertEquals(Format.TEXT_ONLY, emailCaptorValue.getFormat());
}
@Test
public void send_whenDoesSupportHtml_expectHTMLEmailFormat() {
String to = "baeldung@baeldung.com";
String subject = "Great New Course!";
String body = "<html><body>Try out our new course.</html></body>";
emailService.send(to, subject, body, true);
verify(platform).send(emailCaptor.capture());
Email value = emailCaptor.getValue();
assertEquals(Format.HTML, value.getFormat());
}
@Test
public void getServiceStatus_whenServiceRunning_expectUpResponse() {
when(platform.getServiceStatus()).thenReturn("OK");
ServiceStatus serviceStatus = emailService.checkServiceStatus();
assertEquals(ServiceStatus.UP, serviceStatus);
}
@Test
public void getServiceStatus_whenServiceNotRunning_expectDownResponse() {
when(platform.getServiceStatus()).thenReturn("Error");
ServiceStatus serviceStatus = emailService.checkServiceStatus();
assertEquals(ServiceStatus.DOWN, serviceStatus);
}
@Test
public void usingArgumemtMatcher_whenAuthenticatedWithValidCredentials_expectTrue() {
Credentials credentials = new Credentials("baeldung", "correct_password", "correct_key");
when(platform.authenticate(eq(credentials))).thenReturn(AuthenticationStatus.AUTHENTICATED);
assertTrue(emailService.authenticatedSuccessfully(credentials));
}
@Test
public void usingArgumentCapture_whenAuthenticatedWithValidCredentials_expectTrue() {
Credentials credentials = new Credentials("baeldung", "correct_password", "correct_key");
when(platform.authenticate(credentialsCaptor.capture())).thenReturn(AuthenticationStatus.AUTHENTICATED);
assertTrue(emailService.authenticatedSuccessfully(credentials));
assertEquals(credentials, credentialsCaptor.getValue());
}
@Test
public void authenticate_whenNotAuthenticated_expectFalse() {
Credentials credentials = new Credentials("baeldung", "incorrect_password", "incorrect_key");
when(platform.authenticate(credentialsCaptor.capture())).thenReturn(AuthenticationStatus.NOT_AUTHENTICATED);
assertFalse(emailService.authenticatedSuccessfully(credentials));
assertEquals(credentials, credentialsCaptor.getValue());
}
}