Merge pull request #9729 from developerDiv/argumentcaptor
BAEL-4261 - ArgumentMatcher with Stubbing
This commit is contained in:
commit
7e22f6a1bb
@ -0,0 +1,7 @@
|
|||||||
|
package com.baeldung.mockito.argumentcaptor;
|
||||||
|
|
||||||
|
public enum AuthenticationStatus {
|
||||||
|
AUTHENTICATED,
|
||||||
|
NOT_AUTHENTICATED,
|
||||||
|
ERROR
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,10 @@
|
|||||||
|
package com.baeldung.mockito.argumentcaptor;
|
||||||
|
|
||||||
|
public interface DeliveryPlatform {
|
||||||
|
|
||||||
|
void deliver(Email email);
|
||||||
|
|
||||||
|
String getServiceStatus();
|
||||||
|
|
||||||
|
AuthenticationStatus authenticate(Credentials credentials);
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -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.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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,6 @@
|
|||||||
|
package com.baeldung.mockito.argumentcaptor;
|
||||||
|
|
||||||
|
public enum Format {
|
||||||
|
TEXT_ONLY,
|
||||||
|
HTML
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
package com.baeldung.mockito.argumentcaptor;
|
||||||
|
|
||||||
|
public enum ServiceStatus {
|
||||||
|
UP,
|
||||||
|
DOWN,
|
||||||
|
AUTHENTICATED
|
||||||
|
}
|
@ -0,0 +1,93 @@
|
|||||||
|
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));
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user