JAVA-13632 Updated code for Mocking Void Methods with Mockito

This commit is contained in:
Dhawal Kapil 2022-08-09 22:41:27 +05:30
parent 57aa81d552
commit 595aa8fe3b
1 changed files with 14 additions and 8 deletions

View File

@ -1,12 +1,18 @@
package com.baeldung.mockito.argumentcaptor; package com.baeldung.mockito.argumentcaptor;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.*; import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.MockitoJUnitRunner;
import static org.junit.Assert.*;
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
public class EmailServiceUnitTest { public class EmailServiceUnitTest {
@ -32,7 +38,7 @@ public class EmailServiceUnitTest {
Mockito.verify(platform).deliver(emailCaptor.capture()); Mockito.verify(platform).deliver(emailCaptor.capture());
Email emailCaptorValue = emailCaptor.getValue(); Email emailCaptorValue = emailCaptor.getValue();
assertEquals(Format.TEXT_ONLY, emailCaptorValue.getFormat()); assertThat(emailCaptorValue.getFormat()).isEqualTo(Format.TEXT_ONLY);
} }
@Test @Test
@ -45,7 +51,7 @@ public class EmailServiceUnitTest {
Mockito.verify(platform).deliver(emailCaptor.capture()); Mockito.verify(platform).deliver(emailCaptor.capture());
Email value = emailCaptor.getValue(); Email value = emailCaptor.getValue();
assertEquals(Format.HTML, value.getFormat()); assertThat(value.getFormat()).isEqualTo(Format.HTML);
} }
@Test @Test
@ -54,7 +60,7 @@ public class EmailServiceUnitTest {
ServiceStatus serviceStatus = emailService.checkServiceStatus(); ServiceStatus serviceStatus = emailService.checkServiceStatus();
assertEquals(ServiceStatus.UP, serviceStatus); assertThat(serviceStatus).isEqualTo(ServiceStatus.UP);
} }
@Test @Test
@ -63,7 +69,7 @@ public class EmailServiceUnitTest {
ServiceStatus serviceStatus = emailService.checkServiceStatus(); ServiceStatus serviceStatus = emailService.checkServiceStatus();
assertEquals(ServiceStatus.DOWN, serviceStatus); assertThat(serviceStatus).isEqualTo(ServiceStatus.DOWN);
} }
@Test @Test
@ -80,7 +86,7 @@ public class EmailServiceUnitTest {
Mockito.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()); assertThat(credentialsCaptor.getValue()).isEqualTo(credentials);
} }
@Test @Test