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

发布于 2024-09-11 10:28:56 字数 1406 浏览 5 评论 0原文

可能的重复: 单元测试私有方法的最佳方式是什么?

我是一名初学者程序员,我不知道如何编写一个结构良好以进行单元测试的应用程序。我想编写能够随后添加有效单元测试的应用程序。

问题在于 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));
  }
 }
}

解决方案:

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

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

  3. < p>反射 - 如果这使得调用私有方法成为可能,那么它看起来是一个很好的解决方案 http://www.artima.com/suiterunner/private3.html (我应该学习更多知识来理解反射。我不明白如果我们可以从另一个类调用私有方法,那么反射如何不会破坏所有具有公共和私有方法的想法。)

  4. 不定义私有方法< /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:

  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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

后eg是否自 2024-09-18 10:28:56

您不需要测试私有方法。

  • 私有方法是具体实现的一部分。您不应该测试实现,而应该测试功能。如果您测试类公开的功能,则可以根据单元测试更改实现。
  • 如果您觉得需要测试私有方法,这是一个好兆头,表明您应该将私有方法移至另一个类并将该方法设为公共。通过这样做,您可以获得更小的类,并且可以轻松测试这些方法。如果您不想公开这个新类,可以将其设置为包私有(默认访问修饰符)。

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).
眼眸里的快感 2024-09-18 10:28:56

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

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.

野の 2024-09-18 10:28:56

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

  1. 除了显示之外,测试不会证明任何事情某项内部功能正在“工作”,而这对于实际使用您的软件的人来说毫无意义。

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

当然,您可以选择将大型项目细分为较小的功能块,在这种情况下,您可以选择对接口之间的接口进行单元测试(例如,您可以选择对数据访问层进行单元测试,尽管事实上 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:

  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!

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

别忘他 2024-09-18 10:28:56

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

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

初与友歌 2024-09-18 10:28:56

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文