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
1 changed files with 9 additions and 15 deletions

View File

@ -2,16 +2,10 @@ 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.*;
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 {
@ -36,7 +30,7 @@ public class EmailServiceUnitTest {
emailService.send(to, subject, body, false);
verify(platform).send(emailCaptor.capture());
Mockito.verify(platform).send(emailCaptor.capture());
Email emailCaptorValue = emailCaptor.getValue();
assertEquals(Format.TEXT_ONLY, emailCaptorValue.getFormat());
}
@ -49,14 +43,14 @@ public class EmailServiceUnitTest {
emailService.send(to, subject, body, true);
verify(platform).send(emailCaptor.capture());
Mockito.verify(platform).send(emailCaptor.capture());
Email value = emailCaptor.getValue();
assertEquals(Format.HTML, value.getFormat());
}
@Test
public void whenServiceRunning_expectUpResponse() {
when(platform.getServiceStatus()).thenReturn("OK");
Mockito.when(platform.getServiceStatus()).thenReturn("OK");
ServiceStatus serviceStatus = emailService.checkServiceStatus();
@ -65,7 +59,7 @@ public class EmailServiceUnitTest {
@Test
public void whenServiceNotRunning_expectDownResponse() {
when(platform.getServiceStatus()).thenReturn("Error");
Mockito.when(platform.getServiceStatus()).thenReturn("Error");
ServiceStatus serviceStatus = emailService.checkServiceStatus();
@ -75,15 +69,15 @@ public class EmailServiceUnitTest {
@Test
public void usingArgumentMatcher_whenAuthenticatedWithValidCredentials_expectTrue() {
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));
}
@Test
public void usingArgumentCapture_whenAuthenticatedWithValidCredentials_expectTrue() {
public void usingArgumentCaptor_whenAuthenticatedWithValidCredentials_expectTrue() {
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));
assertEquals(credentials, credentialsCaptor.getValue());
@ -92,7 +86,7 @@ public class EmailServiceUnitTest {
@Test
public void whenNotAuthenticated_expectFalse() {
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));
}