单元测试:我应该在方法中的测试部分之后进行验证吗?
我刚刚开始单元测试,对以下问题有点困惑。你能帮我一下吗?
以下是我正在测试的示例服务方法:
public CommandDTO update(UUID uuid, Request request) {
// case 1
if (uuid == null) {
throw new EntityNotFoundException("Not found");
}
final Employee employee = employeeService.findByUuid(uuid);
// case 2
if (employee.getDepartmentUuid == null) {
throw new EntityNotFoundException("Not found");
}
employeeRepository.saveAndFlush(employee);
triggerService.onEmployeeUpdate(uuid);
return CommandDTO.builder().uuid(employee.getUuid()).build();
}
1. 当我为测试案例 1 创建测试方法时,我是否应该只编写一个预期 EntityNotFoundException
的测试?我的意思是我应该在测试异常后编写验证行,如下所示?或者我应该只测试直到我正在测试的部分?因为如果我验证其余部分,在这种情况下,我将为所有其他测试重复相同的行。
@Test
public void test_exception_when_update() {
UUID uuid = null;
Request request = new Request();
assertThrows(EntityNotFoundException.class, () -> {
employeeService.update(uuid, request);
});
/// ???
verify(employeeService, never()).findByUuid(any());
verify(employeeRepository, never()).saveAndFlush(any());
verify(triggerService, never()).onEmployeeUpdate(any());
}
2. 对于上面的服务方法,我想我应该编写以下场景的测试方法:
uuid == null
-->抛出异常employee.getDepartmentUuid
-->抛出异常没有异常,其他3个操作都正常,然后返回
CommandDTO
。那我就做出断言。
这是真的吗?
I have just started Unit Testing and I am a little bit confused regarding to the following issues. Could you help me please?
Here is the sample service method that I am testing:
public CommandDTO update(UUID uuid, Request request) {
// case 1
if (uuid == null) {
throw new EntityNotFoundException("Not found");
}
final Employee employee = employeeService.findByUuid(uuid);
// case 2
if (employee.getDepartmentUuid == null) {
throw new EntityNotFoundException("Not found");
}
employeeRepository.saveAndFlush(employee);
triggerService.onEmployeeUpdate(uuid);
return CommandDTO.builder().uuid(employee.getUuid()).build();
}
1. When I create a test method for testing case 1, should I just write a test for expecting EntityNotFoundException
? I mean should I write verify lines after testing exception as shown below? Or should I just test until the part that I am testing? Because if I verify the rest, in that case I will repeat the same lines for all the other tests.
@Test
public void test_exception_when_update() {
UUID uuid = null;
Request request = new Request();
assertThrows(EntityNotFoundException.class, () -> {
employeeService.update(uuid, request);
});
/// ???
verify(employeeService, never()).findByUuid(any());
verify(employeeRepository, never()).saveAndFlush(any());
verify(triggerService, never()).onEmployeeUpdate(any());
}
2. For the above service method, I think I should write the test methods for the following scenarios:
uuid == null
--> throw exceptionemployee.getDepartmentUuid
--> throw exceptionthere is no exception and the other 3 operations are ok and then return
CommandDTO
. Then I will make assertions.
Is that true?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过两种方式做到这一点。但验证不会造成任何损害。
想象一下场景:
即使未给出
uuid
或发现员工没有departmentUUID
,您的update()
方法也会更改为保存员工。在这种情况下,如果包含验证检查,您就可以捕获这些新更改,因为测试将失败。You can do it in both ways. But verifying won't do any harm.
Imagine the scenario:
your
update()
method is changed to save employee even ifuuid
is not given or found employee doesn't havedepartmentUUID
. In that case, you can catch these new changes if you include your verify-checks, because the test will fail.