TheProgrammer
TheProgrammer

Reputation: 44

How do I tell IntelliJ to do a "full" path coverage sweep?

Suppose I have the following code:

class NetPay {

  /**
   * Computes the net pay of an employee given certain conditions.
   * @param hours - the number of hours worked.
   * @param rate - the hourly rate.
   * @param vacation - whether the employee is on vacation.
   * @param stateTax - whether the employee is subject to state tax.
   * @param federalTax - whether the employee is subject to federal tax.
   * @return the net pay.
   */
  static double computeNetPay(double hours, double rate, boolean vacation,
                              boolean stateTax, boolean federalTax) {
    double grossPay = hours * rate;
    if (vacation) {
      grossPay *= 2;
    }
    if (stateTax) {
      grossPay *= 0.85;
    }
    if (federalTax) {
      grossPay *= 0.80;
    }
    return grossPay;
  }
}

If I have the following tests, IntelliJ, in its code and branch coverage report, says that the tests cover 100% of branches, but this is not true. We're missing five to account for all eight possible outcomes. These are commented out.

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;

class NetPayTester {

  @Test
  void testComputeNetPay() {
    assertEquals(300, NetPay.computeNetPay(20, 15, false, false, false));
    assertEquals(204, NetPay.computeNetPay(20, 15, false, true, true));
    assertEquals(600, NetPay.computeNetPay(20, 15, true, false, false));
//    assertEquals(240, NetPay.computeNetPay(20, 15, false, false, true));
//    assertEquals(255, NetPay.computeNetPay(20, 15, false, true, false));
//    assertEquals(480, NetPay.computeNetPay(20, 15, true, false, true));
//    assertEquals(510, NetPay.computeNetPay(20, 15, true, true, false));
//    assertEquals(408, NetPay.computeNetPay(20, 15, true, true, true));
  }
}

My question is: how can I tell IntelliJ to account for this kind of path coverage?

Upvotes: 1

Views: 125

Answers (3)

Nidhi257
Nidhi257

Reputation: 967

You can use maven jacoco goal and it will generate report as below:

Maven goal - mvn clean org.jacoco:jacoco-maven-plugin:0.8.8:prepare-agent test org.jacoco:jacoco-maven-plugin:0.8.8:report-aggregate

jacoco report location - target/site/jacoco/index.html

enter image description here

enter image description here

Report has column of coverage and branch coverage.

enter image description here

Upvotes: 0

Approach

Let me explain a substantial difference regarding this problem:

  • Branch coverage - Ensures that each branch (resulting from conditional statements like if, else, etc.) is executed at least once during the test.

  • Statement coverage - Ensures that all lines of code are executed at least once during the test.

  • Path Coverage – Ensures that your tests cover all possible combinations of branches. This type of coverage is exponentially more complex than branch or statement coverage.

In summary, branch and statement coverage focuses on whether all lines of code or decisions (branches) have been executed, while path coverage aims to test all possible combinations of decisions within the code. Path coverage is typically the most comprehensive for software testing, but it is also exponentially more complex.

Answer

Therefore, when asked "how can I tell IntelliJ to account for this kind of path coverage?":

Unfortunately, IntelliJ's built-in coverage analysis focuses on branch and statement coverage, which are more common and pragmatic metrics. So in the scenario you described, it is possible to achieve 100% branch coverage even if you haven't tried every possible combination of conditions.

My answer/advice would be to use an external library like JaCoCo to measure path coverage more completely. There is also an integrated plugin for Intellij - see JaCoCo with IntelliJ IDEA - geeksforgeeks

I hope I have been helpful. Happy developing!


Note: As a tip, from JUnit5 you can parameterize the tests for cleaner and more readable development. See - Guide to JUnit 5 Parameterized

Upvotes: 0

cyberbrain
cyberbrain

Reputation: 5075

Short answer: you can't tell IntelliJ to calculate path coverage.


Some more thoughts on the calculation of path coverage:

I don't know of a tool that provides automated measurement of path coverage for Java (and also not for other languages).

The article What is Path Coverage Testing? Is It Important in Software Testing? describes the steps to calculate path coverage of tests manually - and there are a lot steps.

The article has this statement:

Use of Tools

To manage the creation and execution of test cases for complex code, make use of automated testing tools and test case generation tools.

No mentioning on automated measurement...

Maybe you can get help on some of the sub-steps like calculation of the cyclomatic complexity.

You also will find the following statement:

Although achieving 100% path coverage is the ideal objective in theory for thorough testing, in practice it can be very difficult and resource-intensive, especially for complex software systems.

Since there are so many potential paths and so much testing to do, it may not be feasible to aim for 100% path coverage in many real-world situations.

So I would recommend that you read the article, calculate your path coverage manually for your simple example (remember it gets much more complex if you e.g. add a simple loop!), and put away path coverage into your bag of tools, bury it deep down and hope that you never need it in real life.

Upvotes: 1

Related Questions