Contents
Can we test private methods in JUnit?
So whether you are using JUnit or SuiteRunner, you have the same four basic approaches to testing private methods: Don’t test private methods. Give the methods package access. Use a nested test class.
What is the correct process for testing protected methods?
In most languages, you can make a method protected (instead of public), and put the test class in the same package (or whatever), and the method will be available for test. There are annotations that can help, as described by other posters. You can use reflection to get at the private methods (yuck).
When to test private methods in a class?
Proponents of indirect testing of private methods argue that you should only test the public methods of a class. If the public methods make use of private methods, then these private methods automatically get tested when the observable results of the public methods are checked.
Is there a way to test private functions?
The test would look extremely similar, except it would actually compile this time since the GetNextToken () method is now public on the Tokenizer class: Just don’t test the private functions. Sometimes they aren’t worth testing because they will be tested through the public interface.
Can a private method be used in the public interface?
The answer is no. The public interface of the class is still only the public methods in the class. Unless the calling class is in the same package, it can’t use the package private methods. Furthermore, you most likely have direct control over all classes in the same package.
Can a method be unit tested with a private modifier?
It is also too complicated, especially given how easily the methods can be tested if the private modifier is dropped. By removing the private modifier, the method is visible in the package of the class, and nowhere else. This means that the method can now be unit tested (provided that the same package is used in the test class).