Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should I test private methods in Java? [duplicate]

Tags:

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.

like image 674
Ben Avatar asked Jul 21 '10 12:07

Ben


People also ask

Should you test private methods Java?

The short answer is that you shouldn't test private methods directly, but only their effects on the public methods that call them. Unit tests are clients of the object under test, much like the other classes in the code that are dependent on the object.

Is there any direct way to test private method?

To test private methods, you just need to test the public methods that call them. Call your public method and make assertions about the result or the state of the object. If the tests pass, you know your private methods are working correctly.


2 Answers

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).
like image 113
Sjoerd Avatar answered Sep 27 '22 19:09

Sjoerd


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.

like image 35
PaulJWilliams Avatar answered Sep 27 '22 18:09

PaulJWilliams