我是單元測驗的新手,有時我會遇到多種情況。但是,我不確定是否為每個測驗重新模擬或驗證相同的案例。
例如,我正在嘗試為以下服務方法撰寫單元測驗:
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");
}
}
我想我需要為以下場景撰寫測驗:
1.時employeeService.existsByUuid(uuid) == false,則拋出新的EntityNotFoundException。然后驗證employeeService.save()并且departmentService.existsByUuid()永遠不會被呼叫。
2.當employeeService.existsByUuid(uuid) == truethenemployeeService.save()被呼叫并且我斷言這些值時。然后驗證employeeService.save()并且departmentService.existsByUuid()永遠不會被呼叫。
3.什么時候departmentService.existsByUuid() == false再拋出新的EntityNotFoundException。在這個階段,我還模擬employeeService.existsByUuid(uuid),true以便測驗通過第一個條件。但是,我不確定是否需要斷言第二部分;employeeService.save()被呼叫并且我斷言這些值。我是斷言回傳的值還是只是驗證該方法被呼叫了 1 次。因為我已經斷言了它的值,而第三個測驗只是針對第三個條件。
當我們有多個條件并且可能需要一次又一次地重新測驗相同的條件時,對這種情況有什么想法嗎?
uj5u.com熱心網友回復:
您不應該嘗試逐行測驗您的代碼,而是使用涵蓋單個有意義場景的案例。因此,如果您已經有一個檢查條件的案例,您不必在其他測驗案例中重復這些斷言。
在您的示例中,我認為這些可能是核心案例:
- 如果 UUID 不存在,則拋出例外并且不保存員工
- 如果 UUID 存在,則正確保存所有員工欄位
- 如果保存了員工,但員工所在部門不存在,則拋出例外
要測驗它們,您可以執行以下操作:
EmployeeService employeeService = mock(EmployeeService.class);
情況1:
when(employeeService.existsByUuid(employeeUuid)).thenReturn(false);
try {
testObject.create(request);
fail();
}
catch(EntityNotFoundException e) {
verify(employeeService, never()).save(...);
}
案例2:
when(employeeService.existsByUuid(employeeUuid)).thenReturn(true);
when(employeeService.existsByUuid(departmentUuid)).thenReturn(true);
testObject.create(request);
verify(employeeService).save(field1, field2, ...);
案例3:
when(employeeService.existsByUuid(employeeUuid)).thenReturn(true);
when(employeeService.existsByUuid(departmentUuid)).thenReturn(false);
try {
testObject.create(request);
fail();
}
catch(EntityNotFoundException e) {
// success
}
順便說一句,您還可以在 @Test 注釋中指出預期的例外,但是您不能對結果進行任何進一步的檢查:
@Test(expected = EntityNotFoundException.class)
public void test3() {
when(employeeService.existsByUuid(employeeUuid)).thenReturn(true);
when(employeeService.existsByUuid(departmentUuid)).thenReturn(false);
testObject.create(request);
}
uj5u.com熱心網友回復:
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));
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/447582.html
上一篇:如何回傳網頁上元素的出現次數
