0
class MyClass {
    MyErrorServiceList myErrorServiceList; // myErrorServiceList is initialized by some complex method calls
    
    public boolean processErrorServiceList() {
        if (CollectionUtils.NotEmplty(myErrorServiceList)) {
            // do something
            return true;
        } else { 
            return false;
        }
    }
}

How can I pass myErrorServiceList when calling processErrorServiceList() so that both branches of the code are covered by a test case?

4
  • 1
    What did you try that didn't work?
    – daniu
    Commented Jun 21 at 12:35
  • 1
    Have you tried a setter, even if you are using it only internally?
    – N Sarj
    Commented Jun 21 at 13:06
  • How can I write junit test case that bypass (CollectionUtils.NotEmplty(myErrorServiceList)) , currently I am using , when( (CollectionUtils.NotEmplty(any())).thenreturn(true), but this not works and throwing Mockito exception Commented Jun 22 at 5:39
  • 1
    a) redesign your class to be testable b) move method out of class and make public static, so it can be tested independent of class for any kind of list c) use reflection to assign the field d) call your complex methods and have your list initialized. And honestly, I wouldn't test that method separately, it looks like a private implementation detail and should be covered by other tests that perform actual validation of important business logic.
    – knittl
    Commented Jun 22 at 9:12

1 Answer 1

0

use ReflectionTestUtils.setField method

1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Jun 26 at 11:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.