单元测试:我应该在方法中的测试部分之后进行验证吗?

发布于 2025-01-14 16:52:21 字数 1487 浏览 1 评论 0原文

我刚刚开始单元测试,对以下问题有点困惑。你能帮我一下吗?

以下是我正在测试的示例服务方法:

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 exception

  • employee.getDepartmentUuid --> throw exception

  • there is no exception and the other 3 operations are ok and then return CommandDTO. Then I will make assertions.

Is that true?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

弄潮 2025-01-21 16:52:21

您可以通过两种方式做到这一点。但验证不会造成任何损害。
想象一下场景:
即使未给出 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 if uuid is not given or found employee doesn't have departmentUUID. In that case, you can catch these new changes if you include your verify-checks, because the test will fail.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文