我是否需要为没有逻辑的服务方法编写单元测试? [英] Do I need to write unit tests for methods of services without logic?

查看:16
本文介绍了我是否需要为没有逻辑的服务方法编写单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要这样的测试服务方法吗?

Do I need test service methods like this?

@Transactional
@Override
public Charge saveAndFlush(Charge charge) {
    return chargesRepository.saveAndFlush(charge);
}

原则上,没有什么可测试的.因此,问题出现了 - 无论如何都要编写测试,因为将来可能有人会在这里添加逻辑.或者让他写一个会添加逻辑的人(如果有的话).

In principle, there is nothing to test. Therefore, the question arose - to write a test anyway, because in the future it is possible that someone will add logic here. Or let him write the one who will add logic (if at all).

推荐答案

没有什么可测试的

there is nothing to test

嗯,有.因为不做测试无法区分正确的实现和以下错误的实现":

Well, there is. Because doing no tests can not distinguish between the correct implementation and the following faulty "implementations":

{
   return null;
}

{
   return chargesRepository.saveAndFlush(null);
}

{
   return chargesRepository.saveAndFlush(new Charge());
}

{
   return chargesRepository.someOtherMethod(charge);
}

但是您认为要测试的很少是正确的.假设 chargesRepository 类已经被正确测试,你只需要一两个单元测试来证明该方法正确地委托给了 chargesRepository.

But you are right to think there is very little to test. Assuming the class of chargesRepository is already properly tested, you only need one or two unit tests to show that the method correctly delegates to the chargesRepository.

这篇关于我是否需要为没有逻辑的服务方法编写单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆