Junit Best Practice: Public method calling multiple private methods
java, junit, junit4, unit-testing
Solution
Base the number of methods on the number of scenarios you want to test, it has nothing to do with the methods that the thing being tested has.
If each scenario takes its own code to set up, then you will get one test method for each scenario. If you can parameterize the tests then you may be able to have one test method and pass in different data for each scenario.
The important thing is that for each combination of inputs you want the test to succeed or fail independently of the other tests. If you shoehorn all the tests into one method then that can't happen, the first test failure will prevent the remaining tests from running.
Problem
I am starting to write JUnit test cases for a legacy codebase. One of the public methods has multiple if statements and based on a condition it is calling different private methods. Should I write just one test method and test for all the conditions? or one method for each condition? Wouldn't I lose consistency if I write individual methods for each if condition? What is the approach to test private methods? Private method logic could be more complicated than public methods.