0

I have written a piece of code and I have written both unit and integration tests to cover it. The PR is failing because SonarQube says that the line coverage is below the threshold because the if conditions are not covered. On the other hand, IntelliJ is suggesting that the coverage is 100% for this class.

Optional<Book> bookOptional = bookPersistence.findBookFor(book.id);
bookOptional.ifPresent(book -> {
    if (book.title != "something") {
        throw new SomeException("cause");
    }
});
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
s33h
  • 183
  • 1
  • 8
  • 3
    It needs 3 tests to be covered. 1: not existing bookId. 2. existing book id with title is not something. 3 existing book id where title is something. Then assert all these test cases gives the expected result. – zforgo Apr 06 '23 at 07:04
  • 1
    Generally the coverage is considered for line coverage only. here you need to work on condition coverage as a single if statement with only one condition will have two branches one for positive and other for negative. Hence by writing only a single test case either for positive or negative statement coverage will become 100% but the condition coverage will be 50% only. Hope this helps. – vijaydeep Apr 06 '23 at 08:24
  • 1
    Also see https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – tgdavies Apr 06 '23 at 09:02
  • @zforgo that is clear to me and I have unit and IT test written to cover that but does not work with SonarQube. – s33h Apr 11 '23 at 06:38

0 Answers0