我应该如何测试 Java 中的私有方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3299405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 21:52:26  来源:igfitidea点击:

How should I test private methods in Java?

javaunit-testingtestingjunitautomated-tests

提问by Yosef

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 privatemethods - they can't be testing with outside of their classes.

问题在于private方法 - 他们不能在他们的类之外进行测试。

Should I solve this problem by changing all methods that are privateto protected, and let the test class extend source class? Or is there a better solution?

我应该改变是所有的方法解决这个问题privateprotected,让测试类扩展源类?或者有更好的解决方案吗?

My solution (private splitLetters => protected splitLetters) would work like this:

我的解决方案(私有 splitLetters => protected splitLetters)会像这样工作:

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:

解决方案:

  1. 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.

  2. Nested class way to test- problematic because QA make changes in source code

  3. 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.)

  4. Not define private methods(as I showed in my solution) - problematic because sometimes we have to define a private method.

  1. 不测试私有方法- 有时私有方法正在执行非常复杂的任务,应该很好地进行测试,我们不希望该用户可以访问这些方法。很快,解决方案就是将私有方法更改为受保护的方法。

  2. 嵌套类测试方式- 有问题,因为 QA 在源代码中进行了更改

  3. 反射- 如果这可以调用私有方法,它看起来是一个很好的解决方案http://www.artima.com/suiterunner/private3.html(我应该学习更多来理解反射。我不明白反射是如何做的如果我们可以从另一个类调用私有方法,就不会打破拥有公共和私有方法的所有想法。)

  4. 不定义私有方法(如我在我的解决方案中所示) - 有问题,因为有时我们必须定义私有方法。

回答by foret

In my opinion, private methods should not be tested. Tests are for interfaces (in the broad meaning of this word).

在我看来,不应该测试私有方法。测试是针对接口的(在这个词的广义上)。

回答by Kendrick

You shouldn't need to test the private methods. When you test your public methods that should, in theory, also test your private methods.

您不需要测试私有方法。当你测试你的公共方法时,理论上应该也测试你的私有方法。

回答by Sjoerd

You should not need to test private methods.

您不需要测试私有方法。

  • A private method is specifically part of the implementation. You should not test the implemenation, but the functionality. If you test the functionality a class exposes, you can change the implementation while depending on the unit test.
  • If you feel the need to test a private method, this is a good sign that you should move the private method to another class and make the method public. By doing this, you get smaller classes and you can test the methods easily. If you do not want to expose this new class, you can make it package-private (the default access modifier).
  • 私有方法是具体实现的一部分。您不应该测试实现,而应该测试功能。如果您测试类公开的功能,则可以根据单元测试更改实现。
  • 如果您觉得需要测试私有方法,这是一个好兆头,您应该将私有方法移到另一个类并使该方法公开。通过这样做,您可以获得更小的类,并且您可以轻松地测试这些方法。如果您不想公开这个新类,您可以将其设为包私有(默认访问修饰符)。

回答by PaulJWilliams

Testing private methods implies testing implementation, rather than functionality. Think very carefully about whyyou want to test private methods and you may find you dont need to test them at all.

测试私有方法意味着测试实现,而不是功能。仔细考虑为什么要测试私有方法,您可能会发现根本不需要测试它们。

回答by Justin

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:

我个人的观点是,您应该(尽可能)只测试向该功能的最终用户公开的行为,因此您不应该测试私有方法:

  1. 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.

  2. 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!

  1. 该测试并不能证明任何事情,只是表明一个内部功能根据对实际使用您的软件的人来说毫无意义的东西“工作”。

  2. 如果你改变/重构你的内部实现,你会发现你的单元测试开始失败,而实际上公开的外部功能根本没有改变!

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).

当然,您可能会选择将大型项目细分为更小的功能块,在这种情况下,您可能会选择对接口之间的接口进行单元测试(例如,您可能会选择对数据访问层进行单元测试,尽管 DAL实施不会直接影响最终用户)。