Add static class to 'eq' and 'when' for greater readability.

This commit is contained in:
developerDiv 2020-07-21 21:32:40 +01:00
parent 8309e56268
commit ab755bf098

@ -2,16 +2,10 @@ package com.baeldung.mockito.argumentcaptor;
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.*;
import org.mockito.Captor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.MockitoJUnitRunner;
import static org.junit.Assert.*; 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) @RunWith(MockitoJUnitRunner.class)
public class EmailServiceUnitTest { public class EmailServiceUnitTest {
@ -36,7 +30,7 @@ public class EmailServiceUnitTest {
emailService.send(to, subject, body, false); emailService.send(to, subject, body, false);
verify(platform).send(emailCaptor.capture()); Mockito.verify(platform).send(emailCaptor.capture());
Email emailCaptorValue = emailCaptor.getValue(); Email emailCaptorValue = emailCaptor.getValue();
assertEquals(Format.TEXT_ONLY, emailCaptorValue.getFormat()); assertEquals(Format.TEXT_ONLY, emailCaptorValue.getFormat());
} }
@ -49,14 +43,14 @@ public class EmailServiceUnitTest {
emailService.send(to, subject, body, true); emailService.send(to, subject, body, true);
verify(platform).send(emailCaptor.capture()); Mockito.verify(platform).send(emailCaptor.capture());
Email value = emailCaptor.getValue(); Email value = emailCaptor.getValue();
assertEquals(Format.HTML, value.getFormat()); assertEquals(Format.HTML, value.getFormat());
} }
@Test @Test
public void whenServiceRunning_expectUpResponse() { public void whenServiceRunning_expectUpResponse() {
when(platform.getServiceStatus()).thenReturn("OK"); Mockito.when(platform.getServiceStatus()).thenReturn("OK");
ServiceStatus serviceStatus = emailService.checkServiceStatus(); ServiceStatus serviceStatus = emailService.checkServiceStatus();
@ -65,7 +59,7 @@ public class EmailServiceUnitTest {
@Test @Test
public void whenServiceNotRunning_expectDownResponse() { public void whenServiceNotRunning_expectDownResponse() {
when(platform.getServiceStatus()).thenReturn("Error"); Mockito.when(platform.getServiceStatus()).thenReturn("Error");
ServiceStatus serviceStatus = emailService.checkServiceStatus(); ServiceStatus serviceStatus = emailService.checkServiceStatus();
@ -75,15 +69,15 @@ public class EmailServiceUnitTest {
@Test @Test
public void usingArgumentMatcher_whenAuthenticatedWithValidCredentials_expectTrue() { public void usingArgumentMatcher_whenAuthenticatedWithValidCredentials_expectTrue() {
Credentials credentials = new Credentials("baeldung", "correct_password", "correct_key"); Credentials credentials = new Credentials("baeldung", "correct_password", "correct_key");
when(platform.authenticate(eq(credentials))).thenReturn(AuthenticationStatus.AUTHENTICATED); Mockito.when(platform.authenticate(Mockito.eq(credentials))).thenReturn(AuthenticationStatus.AUTHENTICATED);
assertTrue(emailService.authenticatedSuccessfully(credentials)); assertTrue(emailService.authenticatedSuccessfully(credentials));
} }
@Test @Test
public void usingArgumentCapture_whenAuthenticatedWithValidCredentials_expectTrue() { public void usingArgumentCaptor_whenAuthenticatedWithValidCredentials_expectTrue() {
Credentials credentials = new Credentials("baeldung", "correct_password", "correct_key"); Credentials credentials = new Credentials("baeldung", "correct_password", "correct_key");
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()); assertEquals(credentials, credentialsCaptor.getValue());
@ -92,7 +86,7 @@ public class EmailServiceUnitTest {
@Test @Test
public void whenNotAuthenticated_expectFalse() { public void whenNotAuthenticated_expectFalse() {
Credentials credentials = new Credentials("baeldung", "incorrect_password", "incorrect_key"); Credentials credentials = new Credentials("baeldung", "incorrect_password", "incorrect_key");
when(platform.authenticate(eq(credentials))).thenReturn(AuthenticationStatus.NOT_AUTHENTICATED); Mockito.when(platform.authenticate(Mockito.eq(credentials))).thenReturn(AuthenticationStatus.NOT_AUTHENTICATED);
assertFalse(emailService.authenticatedSuccessfully(credentials)); assertFalse(emailService.authenticatedSuccessfully(credentials));
} }