Index
- Junit
- Mockito
- testing private method (reflection ,powermock)
2 testing fameworks- MOckito abd Sping testing
| MOck | MOckBean | |
|---|---|---|
| annotation | pary of Mockitoframework | SPring testing framework |
| framework | @RunWith(MockitoJUnitRunner.class) public class MyServiceTest { @Mock private MyRepository myRepository; | @SpringBootTest public class MyServiceIntegrationTest { @Autowired private MyService myService; @MockBean private MyRepository myRepository; |
| Purpose | UnitTetsing | Integration testing |
Spy vs Mock
| @spy | @Mock | |
|---|---|---|
| Functionality | A partially mocked version of a real object. | A complete replacement for a real object. |
| Creation | Wraps an existing object | Creates a new object |
| Control | Partial control (can define specific behaviors for specific methods) | Full control over behavior |
| Use case | Testing interactions within a real object with some real behavior | Isolating dependencies, unit testing with specific behavior |
| access | can access private methods of the original object | cannot access private methods of the original object |
| example | below code | below code // |
Mock usage
// Interface we want to test
public interface EmailService {
void sendEmail(String recipient, String message);
}
// Test class using mock
@Test
public void testSendEmail() {
// Create a mock object of EmailService
EmailService mockEmailService = Mockito.mock(EmailService.class);
// Define behavior for the mock object
Mockito.when(mockEmailService.sendEmail("user@example.com", "Hello world!")).thenReturn(true);
// Use the mock object in your test logic
myService.sendNotification(mockEmailService, "user@example.com", "Hello world!");
// Verify interactions with the mock object
Mockito.verify(mockEmailService).sendEmail("user@example.com", "Hello world!");
}
IN above example :
- We create a mock object of
EmailServiceusingMockito.mock. - We define behavior for the
sendEmailmethod usingMockito.when. Here, it always returnstrue. - We use the mock object in the test and verify its interaction later.
Spy usage
// Real implementation of EmailService
public class RealEmailService implements EmailService {
@Override
public void sendEmail(String recipient, String message) {
// Send email logic goes here (not shown for simplicity)
}
}
// Test class using spy
@Test
public void testSendEmailWithSpy() {
// Create a real object
EmailService realEmailService = new RealEmailService();
// Create a spy object from the real object
EmailService spyEmailService = Mockito.spy(realEmailService);
// Define behavior for specific method (optional)
Mockito.when(spyEmailService.sendEmail("admin@example.com", "Alert!")).thenReturn("Sent successfully");
// Use the spy object in your test logic
myService.sendNotification(spyEmailService, "user@example.com", "Hello world!");
myService.sendNotification(spyEmailService, "admin@example.com", "Alert!");
// Verify interactions (optional)
Mockito.verify(spyEmailService, times(2)).sendEmail(anyString(), anyString());
}
IN above example :
- Create a real
RealEmailServiceobject. - Create a spy of the
RealEmailServiceusingMockito.spy. - Define behavior for the
sendEmailmethod to return a specific message for “admin@example.com” email. - Use the spy object and verify interactions (optional).
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.7.4</version>
</dependency>
https://www.learnbestcoding.com/post/21/unit-test-private-methods-and-classes
REfernce