-1

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");
}
Nick
  • 43
  • 8
  • Neither of your functions returns anything. Why not have `compareRuleRanks` return `true/false` as well as printing its result, and have `compare()` return that value as well. Then in your test, you can test the return value of `compare()`. – CryptoFool Sep 24 '22 at 21:10

1 Answers1

0

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.

Queeg
  • 7,748
  • 1
  • 16
  • 42