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));
}
}
}
Edit:
solutions :
1.Not test private methods - Sometimes private method doing very compicated tasks that should be tested very well and we dont want that user will have access to this methods. Soo the solution is change private methods to protected.
2.Nested class way to test - problematic because QA make changes in source code
3.Reflaction - If this make possible to call for private methods, its looks great solution http://www.artima.com/suiterunner/private3.html
( I should learn more to understand reflaction,I don't understand how reflactions not breaks all idea of public, private if we can to call to private methods from other class.)
4.Not define private methods (as i showed in my solution) - problematic because sometemies we have to define private method.