Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Indicate in Code which methods/classes are excluded from code coverage instead of reducing the required coverage ratio #1597

Open
axthosarouris opened this issue Mar 23, 2024 · 0 comments

Comments

@axthosarouris
Copy link

axthosarouris commented Mar 23, 2024

THIS IS A BUG TRACKER ONLY. FOR QUESTIONS PLEASE CHECK FAQ OR USE FORUM:

http://www.jacoco.org/jacoco/trunk/doc/faq.html

https://groups.google.com/forum/?fromgroups=#!forum/jacoco

Please understand that
ISSUES WITHOUT FOLLOWING INFORMATION WILL BE CLOSED WITHOUT COMMENTS!
Thank you for filling feature request!

Scenario

  • JaCoCo version: 0.8.11
  • Operating system: Ubuntu Linux
  • Tool integration: Maven / Ant / CLI / API (for others please report to respective project)
  • Description of your use case: (detailed description or executable reproducer, e.g. GitHub repo)

Current Behaviour

In some rare cases, some functions/classes cannot be tested. In such cases, the developer has two choices: a) to reduce the coverage ratio, and b) to indicate the function/class as generated. Both choices have their problems: The first one allows for actual functionality to go untested, while the second is misinforming.

Example:
In the following AWS Lambda handler, the default constructor cannot be tested offline because it will attempt a connection with the online source (AWS).

public class InitFunction implements RequestStreamHandler {
  
  private final CodePipelineClient client;

  public InitFunction() {
    this(defaultCodePipelineClient());
  }

  public InitFunction(CodePipelineClient client) {
    this.client = client;
  }

  @Override
  public void handleRequest(InputStream input, OutputStream output, Context context)
    throws IOException {
    var event = JsonConfig.JSON.readValue(input, CodePipelineEvent.class);
    var request =
      PutJobSuccessResultRequest.builder()
        .jobId(event.getJob().getJobId()).build();
    client.putJobSuccessResult(request);
  }

  private static CodePipelineClient defaultCodePipelineClient() {
    return CodePipelineClient.create();
  }
}

Currently, assuming that the requirement for method and class coverage is 100%, Jacoco will complain that the default constructor is not covered. Unfortunately, the default constructor cannot be tested offline because the client attempts to create an internet connection to AWS,

Wanted Behaviour

The desired behavior, is to be able to manually indicate which functions we cannot cover, (either for working on covering them later, or for showing to other developers that this function is not covered). Based on the above example, we would write
to indicate that the function should be ignored.

public class InitFunction implements RequestStreamHandler {
  
  private final CodePipelineClient client;

  @JacocoIgnored
  public InitFunction() {
    this(defaultCodePipelineClient());
  }

  public InitFunction(CodePipelineClient client) {
    this.client = client;
  }

  @Override
  public void handleRequest(InputStream input, OutputStream output, Context context)
    throws IOException {
    var event = JsonConfig.JSON.readValue(input, CodePipelineEvent.class);
    var request =
      PutJobSuccessResultRequest.builder()
        .jobId(event.getJob().getJobId()).build();
    client.putJobSuccessResult(request);
  }

  @JacocoIgnored
  private static CodePipelineClient defaultCodePipelineClient() {
    return CodePipelineClient.create();
  }
}

Possible Workarounds

One can define an @JacocoGenerated annotation and include it in their code.

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

No branches or pull requests

1 participant