将私有方法更改为受保护以进行测试 [英] Changing private methods to protected for testing

查看:129
本文介绍了将私有方法更改为受保护以进行测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将私有方法更改为受保护的JUNIT测试是否是个好主意。?

Is it a good idea to change the private methods to protected for JUNIT testing.?

推荐答案

它有时很有用,是的。

如果类是可扩展的,请确保使方法最终。

If the class is extendable, make sure to make the method final.

此外,记录事实该方法不应该由同一个包的子类或外部类调用。

Also, document the fact that the method is not supposed to be called by subclasses or external classes of the same package.

我使用Guava @VisibleForTesting注释来明确该方法应该在事实上是私人的。

I use the Guava @VisibleForTesting annotation to make it clear that the method should in fact be private.

这篇关于将私有方法更改为受保护以进行测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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