JUnit test case to cover multiple stream filter conditions - SonarQube code coverage

353 Views Asked by At

Working on SonarQube code coverage & stuck with below conditional filter from stream. I need to write Junit such as both conditions will get covered and should cover this line as part of sonar. Quick help is appreciated. Let me know if you need more details on this. I am trying to mock below conditions in junit, but filtered line is not being covered. Sorry but cannot upload full code as per company policy, but please comment if you need more clarity on this or any references on internet.

private Optional<MainClass> mainMethod() {
        return classObject.stream()
                .filter(r -> r.salary() == 1000 || r.salary() == 2000)   //Need to cover both conditions as part of SonarQube code coverage
        .findAny();
    }

Tried to mock classObject with

  1. when(mockClassObject.salary()).thenReturn(1000) - Expecting to cover first condition.

  2. when(mockClassObject.salary()).thenReturn(2000) - Expecting to cover second condition.
    
  3. when(mockClassObject.salary()).thenReturn(10) - Uncovered condition
    

but it doesn't seems to be covering 3 conditions.

0

There are 0 best solutions below