Issue
I am new in Unit Testing and I have sometimes such situations with multiple conditions. However, I am not sure if I re-mock or verify the same cases for each test.
For example, I am trying to write Unit Tests for the following service method:
public void create(Request request) {
// code omitted
if (!employeeService.existsByUuid(uuid)) {
throw new EntityNotFoundException("Not found");
}
EmployeeDTO employee = employeeService.save(...);
if (!departmentService.existsByUuid(employee.getDepartment())) {
throw new EntityNotFoundException("Not found");
}
}
I think I need to write my tests for the following scenarios:
1. when employeeService.existsByUuid(uuid) == false
, then throw new EntityNotFoundException. then verify employeeService.save()
and departmentService.existsByUuid()
is never called.
2. when employeeService.existsByUuid(uuid) == true
then employeeService.save()
is called and I assert the values. and then verify employeeService.save()
and departmentService.existsByUuid()
is never called.
3. when departmentService.existsByUuid() == false
then throw new EntityNotFoundException. At this stage, I also mock employeeService.existsByUuid(uuid)
as true
so that test passes the first condition. However, I am not sure if do I need to assert the second part; employeeService.save()
is called and I assert the values. Do I assert of the returned values or just verify that method is called 1 time. Because I already asserted its value and the 3rd test is just for the 3rd condition.
Any idea for this kind of scenarios when we have multiple condition and may need to re-test the same condition again and again?
Solution
You can use mockito verify and assert throws to test your objectives something like below
@Test
public void testOne(){
when(employeeService.existsByUuid(uuid)).thenReturn(false);
assertThrows(EntityNotFoundException.class, () -> {
create(request);
});
verify(employeeService, times(0)).save(eq(empObj));
verify(departmentService, times(0)).existsByUuid(eq(departmentObj));
}
@Test
public void testTwo(){
when(employeeService.existsByUuid(uuid)).thenReturn(true);
when(departmentService.existsByUuid(uuid)).thenReturn(true);
create(request);
verify(employeeService, times(1)).save(eq(empObj));
verify(departmentService, times(1)).existsByUuid(eq(departmentObj));
}
@Test
public void testThree(){
when(employeeService.existsByUuid(uuid)).thenReturn(true);
when(departmentService.existsByUuid(uuid)).thenReturn(false);
assertThrows(EntityNotFoundException.class, () -> {
create(request);
});
verify(employeeService, times(1)).save(eq(empObj));
verify(departmentService, times(1)).existsByUuid(eq(departmentObj));
}
Answered By - HariHaravelan
Answer Checked By - Marilyn (JavaFixing Volunteer)