Skip to content

Ignore switch case default cases which can't be covered #1211

Open

Description

@Hollerweger

Scenario

  • JaCoCo version: (from right bottom corner of JaCoCo report)
    • 0.8.6
  • Operating system:
    • Windows
  • Tool integration: Maven / Ant / CLI / API
    • Maven
  • Description of your use case:

Java Switch default cased can't be convered if all other cases are covered:

switch (enum) {
	case CASE_1:
		doSomething();
		break;
	case CASE_2:
		doSomethingElse();
		break;
	default:
               //Throw Exception in case a new Enum value is added in future and not yet supported
               throw new UnsupportedOperationException("Not supported enum: " + enum.getName());
}

Jacoco should add an option to ignore/mark switch default cases as covered.

Current Behaviour

Switch default cases are makred as uncovered in case all other cases are covered even if coverage is not possible.

Wanted Behaviour

Jacoco should add an option to ignore/mark switch default cases as covered.
If possible Jacoco can check if all other cases are covered first, if not then a simple config option would be enough.

Possible Workarounds

Add unwanted Enums like UNKOWN or DEFAULT and try to cover default case with them.

Activity

  1. Copainbig commented on Aug 7, 2021

    @Copainbig

    I can look at that, but are we sure we want to do this, since here (PR-923), it was decided to not filter user code?

  2. deleted a comment from mukuldhariwal94 on Jun 28, 2022
  3. deleted a comment from BobanNikolov on Jun 28, 2022
  4. jorisgeer commented on Dec 7, 2022

    @jorisgeer

    Result is a falsely reported missed branch. I would classify it as a bug. JaCoco assumes an value outside the enum class has to be handled. But being outside the class definition, you cannot generate it.

  5. wrayphi commented on Jan 5, 2023

    @wrayphi

    It would be great if this could be filtered as it is impossible to cover via test and means that our coverage stats have false positives that we cannot address.
    Also applies to the newer style switch for example:
    `
    enum Example {
    ONE,
    TWO
    }

    class ImpossibleToCover {

    static String switchWithBranchThatCannotBeCovered(Example e) {
    return switch(e) {
    case ONE -> "one";
    case TWO -> "two";
    };
    }
    }
    `

  6. deleted a comment from M4urici0GM on May 9, 2023
  7. ddatsh commented on May 15, 2023

    @ddatsh

    any progress?

  8. M4urici0GM commented on May 15, 2023

    @M4urici0GM

    any updates?
    (idk why deleted my comment)

  9. marchof commented on May 15, 2023

    @marchof
    Member

    @ ddatsh @M4urici0GM Please understand that all progress is documented here. Such only distract maintainers from the actual work. That's why we cleanup our issues from useless clutter.

  10. daniel-frak commented on Jun 23, 2023

    @daniel-frak

    Just chiming in to add that this issue extends to Java 14's switch expressions, making it impossible to have 100% code coverage if they are used, due to the implicit default value (which is unreachable).

  11. forresthopkinsa commented on Jul 26, 2023

    @forresthopkinsa

    Related to #771 and fixed in #1472

  12. nioncode commented on Apr 29, 2024

    @nioncode

    This is still broken for multi-line switch cases I think, see: #1472 (comment)

    Works fine in jacoco v0.8.12.

  13. duracotton commented on May 14, 2024

    @duracotton

    For me using Jacoco 0.8.12 it's still showing a missed branch:
    image

  14. byte-artisan commented on Jun 26, 2024

    @byte-artisan

    Since the issue is still in under triage, I would like to point another impact. In case the project use coverage minimal limits, for presented Switch, with 4 branches including default, it is not possible to set coverage limit over than 75%, which is a low limit.

  15. moved this to Awaiting triage in Filteringon Jul 11, 2024
  16. Hollerweger commented on Apr 4, 2025

    @Hollerweger
    Author

    Related to #771 and fixed in #1472

    @forresthopkinsa Is this not yet released or was it wrongly stated that it should fix this issue?

  17. shibicr93 commented on Jan 29, 2026

    @shibicr93

    Dear maintainers, could this issue be prioritized? It is open for a very long time and it is very annoying to use jacoco with the latest developments in the switch expression.

  18. shibicr93 commented on Jan 29, 2026

    @shibicr93

    I'm more than happy to lend a hand :-)

  19. forresthopkinsa commented on Jan 30, 2026

    @forresthopkinsa

    Is this not yet released or was it wrongly stated that it should fix this issue?

    There are two different issues being described here. The one that was fixed was exhaustive switches incorrectly reporting an uncovered default case. The one you're talking about is exhaustiveness inference which Jacoco still does not do.

  20. marchof commented on Jan 31, 2026

    @marchof
    Member

    To manage expectation: JaCoCo analyzes the class files in scope one by one. JaCoCo has no "class loader" and does not analyze the structure of other classes (e.g. from libraries). This would be required to decide whether the switch on enum is exhaustive (for the specific version of that enum class).

    As exhaustive switch expressions are properly handled by JaCoCo I tend to close this as "wont fix".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    • Status
      Awaiting triage

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions