How to capture the error message in private function

Viewed 14

I have a function compare which called private function compareRuleRanks, and println the error or correct message when condition met, how can I capture the message when I do unit test? I try to use AssertionError but didn't work, how can I do that?

AssertionError(dataCompare.compare("Rule1", "Rule2"));

public void compare(){
       compareRuleRanks(rule1, rule2);
}

private void compareRuleRanks(rule1, rule2) {
   if(rule1.rank != rule2.rank) {
     println("The ranks are not in order");
   }
   println("rules are same");
}
1 Answers

When unit-testing compareRulesRanks, you can definitely call the function but you would have to measure whether the intended reaction was triggered.

That means you would have to check whether println() got called as expected. If you cannot do that you probably have to improve your code for testability.

Related