在单元测试中从不同方法调用的方法中测试相同的情况?

发布于 2025-01-13 05:48:15 字数 705 浏览 2 评论 0原文

我有一个私有服务方法,它是从同一服务中的创建和更新方法调用的。通常我们不会直接为私有方法编写单元测试,而是在调用该服务方法的方法中编写案例。

在这个阶段,我很困惑;我在创建方法的测试中编写了私有方法的所有测试用例。

1. 在这个场景中,我在为 update 方法编写测试方法时是否应该省略私有方法的相同情况?因为在这种情况下,它似乎正在为同一情况编写冗余测试(从创建调用它+从更新方法再次调用它)。

2. 另一方面,我也在想,如果将来我删除create或update方法,那么计算方法测试也会被删除。在这种情况下,最好在create和update方法中都编写与calculate方法相关的测试用例。

关于这个问题有什么想法吗?

如果您需要根据此场景进行解释,这里有一个示例代码:

public void create(EmployeeRequest request) {
    // code omitted

    calculate();
}


public void update(UUID uuid, EmployeeRequest request) {
    // code omitted

    calculate();
}
private void calculate() {    
    // code omitted
}

I have a private service method that is called from create and update methods in the same service. Normally we do not write Unit Test directly for the private method and I write the case in the method that calls this service method.

At this stage, I am confused that; I write all the test cases for the private method in the tests for create method.

1. In this scene, should I omit the same case for the private method when I write test method for the update method? Because in this case it seems to be writing redundant tests for the same case (calling it from create + calling it again from update method).

2. On the other hand, I am also thinking of that if I remove create or update method in the future, then calculate method tests will be removed. In this case, it may be better to write test cases related to calculate method in both of create and update methods.

Any idea regarding to this issue?

Here is a sample code if you need to explain something based on this scenario:

public void create(EmployeeRequest request) {
    // code omitted

    calculate();
}


public void update(UUID uuid, EmployeeRequest request) {
    // code omitted

    calculate();
}
private void calculate() {    
    // code omitted
}

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文