我应该如何测试 Java 中的私有方法?
可能的重复: 单元测试私有方法的最佳方式是什么?
我是一名初学者程序员,我不知道如何编写一个结构良好以进行单元测试的应用程序。我想编写能够随后添加有效单元测试的应用程序。
问题在于 private
方法 - 它们无法在类之外进行测试。
我是否应该通过将所有 private
方法更改为 protected
并让测试类扩展源类来解决这个问题?或者有更好的解决方案吗?
我的解决方案(private splitLetters => protected splitLetters)将像这样工作:
源类:
class MyClass{
protected splitLetters(int num){
return num+2;
}
}
测试类:
class Test_MyClass extend MyClass{
public splitLettersTest(){
for(int i=0;i<100;i++){
System.println(parent.splitLetters(i));
}
}
}
解决方案:
不测试私有方法 - 有时,私有方法正在执行非常复杂的任务,应该对其进行很好的测试,并且我们不希望该用户有权访问该方法。很快,解决方案就是将私有方法更改为受保护。
嵌套类测试方式 - 有问题,因为 QA 在源代码中进行更改
- < p>反射 - 如果这使得调用私有方法成为可能,那么它看起来是一个很好的解决方案 http://www.artima.com/suiterunner/private3.html (我应该学习更多知识来理解反射。我不明白如果我们可以从另一个类调用私有方法,那么反射如何不会破坏所有具有公共和私有方法的想法。)
不定义私有方法< /strong> (正如我在我的解决方案中所示) - 有问题,因为有时我们必须定义私有方法。
Possible Duplicate:
What’s the best way of unit testing private methods?
I am a beginner programmer, and I don't know how to write an application that will be well structured for unit testing. I want to write applications with the ability to afterwards add effective unit tests.
The problem is with private
methods - they can't be testing with outside of their classes.
Should I solve this problem by changing all methods that are private
to protected
, and let the test class extend source class? Or is there a better solution?
My solution (private splitLetters => protected splitLetters) would work like this:
Source class:
class MyClass{
protected splitLetters(int num){
return num+2;
}
}
Test class:
class Test_MyClass extend MyClass{
public splitLettersTest(){
for(int i=0;i<100;i++){
System.println(parent.splitLetters(i));
}
}
}
Solutions:
Not testing private methods - Sometimes a private method is doing very complicated tasks that should be tested very well, and we don't want that user will have access to this methods. Soon the solution is changing private methods to protected.
Nested class way to test - problematic because QA make changes in source code
Reflection - If this makes it possible to call private methods, it looks like a great solution http://www.artima.com/suiterunner/private3.html
(I should learn more to understand reflection. I don't understand how reflections do not break all the idea of having public and private methods if we can call private methods from another class.)Not define private methods (as I showed in my solution) - problematic because sometimes we have to define a private method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您不需要测试私有方法。
You should not need to test private methods.
测试私有方法意味着测试实现,而不是功能。仔细思考为什么您想要测试私有方法,您可能会发现您根本不需要测试它们。
Testing private methods implies testing implementation, rather than functionality. Think very carefully about why you want to test private methods and you may find you dont need to test them at all.
我个人的观点是,您应该(只要可能)只测试向该功能的最终用户公开的行为,因此您不应该测试私有方法:
除了显示之外,测试不会证明任何事情某项内部功能正在“工作”,而这对于实际使用您的软件的人来说毫无意义。
如果您更改/重构您的内部实现,您可能会发现您的单元测试开始失败,而实际上公开的外部功能根本没有更改!
当然,您可以选择将大型项目细分为较小的功能块,在这种情况下,您可以选择对接口之间的接口进行单元测试(例如,您可以选择对数据访问层进行单元测试,尽管事实上 DAL实施不会直接影响最终用户)。
My personal view is that you should (wherever possible) only test behaviour that is exposed to the end user of that piece of functionality, and therefore that you should not test private methods:
The test doesn't prove anything except to show that a piece of internal functionality is "working" according to something that makes no sense to the people actually using your software.
If you change / re-factor your internal implementation you could find that your unit tests start failing when in fact the external functionality exposed has not changed at all!
Of course you may choose to subdivide large projects up into smaller chunks of functionality, in which case you might choose to unit test the interfaces between the interfaces (for example you might choose to unit test your data access layer, despite the fact that the DAL implementation doesn't directly impact the end user).
您不需要测试私有方法。理论上,当您测试公共方法时,也应该测试您的私有方法。
You shouldn't need to test the private methods. When you test your public methods that should, in theory, also test your private methods.
在我看来,私有方法不应该被测试。测试是针对接口的(在这个词的广义上)。
In my opinion, private methods should not be tested. Tests are for interfaces (in the broad meaning of this word).