Is it possible to get Jacoco to stop complaining about missing "else" statements?

StackOverflow https://stackoverflow.com/questions/23536643

  •  17-07-2023
  •  | 
  •  

Domanda

While not really a problem, it would be nice if there was a setting that could change this.

Basically, I'm working with a bunch of Form fields that either get uploaded or they don't.

I check if the uploaded field is null and if not, I do something with it.

If the field is null... then nothing. Nothing happens, it can stay null. This is for database insertion where certain fields are allowed to be null.

Here's some pseudo-code:

if(randomfield != null) {
    //do something with randomField before insertion in to the DB.
}

Jacoco is constantly giving me the message that it could not check the other possibility, meaning it's complaining about there being no else statement. But there's never going to be one. Is it possible to get Jacoco to stop checking this and if so, where can I read about it? Google search has not been helpful.

È stato utile?

Soluzione

Jacoco is a coverage tool. It checks if every possible branch of the code has been covered. It doesn't complain that there is no else statement. What it complains about is that you've never executed the code with randomField being null.

Maybe the rest of the code will break if randomField is null. Your tests aren't testing this possibility, and that's what jacoco is warning you about. Here's an example:

String foo = null;
if(randomfield != null) {
    foo = randomField;
}
System.out.println(foo.toLowerCase());

If you test the code with randomField being "hello", everything will go fine. But if randomField is null, then this will cause an undesired NullPointerException. That's a bug that would be detected by testing the code with a null randomField. Since you didn't do that, jacoco is complaining.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top