我应该在 Spring Service 中单元测试私有方法吗? [英] Should I unitTest private method in Spring Service?

查看:29
本文介绍了我应该在 Spring Service 中单元测试私有方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 TDD,对此我有一些疑问.

  1. 我应该在 Springmvc 服务中测试私有方法吗?在其他相关问题中,测试私有方法有两个要点,一个是否定的,因为它可能会破坏包装;一个是肯定的,一些私有方法可能真的很重要.我的问题是,Spring Service 中的私有方法有时是组成一个完整功能的独立部分,它们有自己的功能,在我看来,Spring Service 中的这些私有方法与普通对象类中的私有方法不同,我倾向于测试

  2. Spring Service 中的私有方法应该使用protected 还是package-protected 级别?当我使用 Mockito 时,文档说

<块引用>

这很容易解决——只需将方法的可见性从私有更改为包保护(或受保护).我无法理解,因为在服务中看到受保护/包保护的方法真的很少见.

解决方案

使用测试驱动开发 (TDD),您可以在实现之前编写测试.因此,您不能(立即)拥有任何私有方法.在 TDD 中,您只需添加足够的代码即可通过所有测试.

稍微复杂一点的观点是,最常见的 TDD 流程是 红绿重构.redgreen 短语仍然不能产生任何私有方法,但是在重构阶段,您可以自由地提取一些现有代码(已经被测试覆盖)到私有辅助方法,只要你不破坏任何测试.

因此,TDD 可以生成私有方法,但它们已经被测试覆盖.因此,在遵循 TDD 时询问私有方法是否应该被测试覆盖是没有意义的.在 TDD 中,所有生产代码都由测试覆盖,因为除非要通过测试,否则不会编写生产代码.

在实践中,应用程序代码库需要一些Humble Objects 和其他基础设施代码,你不能轻易地进行单元测试.不过,这并不是 TDD 流程的真正组成部分.

无论如何,你不应该尝试直接单元测试内部实现细节.如果可能,私有方法应该通过被测系统的公共 API 覆盖.

I was learning about TDD, I have few questions about it.

  1. Should I test private method in Springmvc Service? In other related questions, there are two main point about testing private method, one is negative because it may break the packaging; one is postive that some private method maybe really important. My question is, private method in Spring service sometimes are independent parts that make up a complete function, they have their own function, in my opinion, these private methods in spring Service are different from those in normal Object Classes, and I tend to test them。

  2. Should I use protected or package-protected level for private methods in Spring Service? When I use Mockito, the document says

It is very easy to work around - just change the visibility of method from private to package-protected (or protected). I can't understand it because it is really rare to see protected/package-protected mthods in service.

解决方案

With test-driven development (TDD) you write the test before the implementation. From this it follows that you can't (immediately) have any private methods. In TDD, you add only enough code to pass all tests.

A slightly more sophisticated view is that the most common TDD process is red-green-refactor. The red and green phrases still can't produce any private methods, but in the refactor phase, you are free to extract some of the existing code (already covered by tests) to private helper methods, as long as you don't break any tests.

Thus, TDD can produce private methods, but they will already be covered by tests. Thus, asking whether private methods should be covered by tests when following TDD makes little sense. In TDD, all production code is covered by tests, because no production code is written unless it's to make a test pass.

In practice, an application code base will need some Humble Objects and other infrastructure code that you can't easily unit test. That's not really part of the TDD process, though.

In any case, you shouldn't attempt to directly unit test internal implementation details. Private methods should, if at all possible, be covered through the System Under Test's public API.

这篇关于我应该在 Spring Service 中单元测试私有方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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