[SOLVED] Branch Coverage, Statement Coverage and Path Coverage | Java Unit Testing

Create a directory called “TestClassValue” Hereafter, we call this directory <dir.

Create a Java class com.work.sample.FlawedClass in directory <dir/src. (The actual path will obviously reflect the package structure.)

100% Branch Coverage & 100% Statement Coverage

Add to the class a method called flawedMethodl that contains a division by zero fault such that it


  1. is possible to create a test suite that achieves 100% branch coverage and does not reveal the fault

  2. every test suite that achieves 100% statement coverage reveals the fault.


Conversely, if you were able to create the method, then create two JUnit test classes com.work.sample.FlawedClassTestBCl and com.work.sample.FlawedClassTestSCl for class FlawedClass as follows:


  1. FlawedClassTestBCl should achieve 100% branch coverage of flawedMethodl and not reveal the fault therein.

  2. FlawedClassTestSCl should achieve 100% statement coverage of flawedMethodl and reveal the fault therein.

  3. Both classes should be saved in directory <dir/test. (Also, in this case, the actual path will obviously reflect the package structure, and the same holds for the test classes in the subsequent tasks.)



100% Statement Coverage & 100% Branch Coverage

Add to the class a method called flawedMethod2 that contains a division by zero faults such that

it is possible to create a test suite that achieves 100% statement coverage and does not reveal the fault
it is not possible to create a test suite that achieves less than 100% branch coverage and reveals the fault (i.e. every test suite which reveals the fault achieves 100% branch coverage).

Conversely, if you were able to create the method, then create two JUnit test classes com.work.sample.FlawedClassTestSC2 and com.work.sample.FlawedClassTestBC2 for class FlawedClass as follows:


  1. FlawedClassTestSC2 should achieve 100% statement coverage of flawedMethod2 and not reveal the fault therein.

  2. FlawedClassTestBC2 should achieve 100% branch coverage of flawedMethod2 and reveal the fault therein.

  3. Both classes should be saved in directory <dir/test.



100% Path Coverage & 100% Branch Coverage

Add to the class a method called flawedMethod3 that contains a division by zero faults such that


  1. every test suite that achieves 100% path coverage reveals the fault and

  2. (2) it is possible to create a test suite that achieves 100% branch coverage and does not reveal the fault.

  3. Conversely, if you were able to create the method, then create two JUnit test classes com.work.sample.FlawedClassTestPC3 and com.work.sample.FlawedClassTestBC3 for class FlawedClass as follows:

  4. FlawedClassTestPC3 should achieve 100% path coverage of flawedMethod3 and reveal the fault therein.

  5. FlawedClassTestBC3 should achieve 100% branch coverage of flawedMethod3, and not reveal the fault therein.

  6. Both classes should be saved in directory <dir/test.



100% Branch Coverage & 100% Statement Coverage

Add to the class a method called flawedMethod4 that contains a division by zero faults such that


  1. it is possible to create a test suite that achieves 100% branch coverage and does not reveal the fault

  2. it is possible to create a test suite that achieves less than 100% statement coverage and reveals the fault.


Conversely, if you were able to create the method, then create two JUnit test classes com.work.sample.FlawedClassTestBC4 and com.work.sample.FlawedClassTestSC4 for class FlawedClass as follows:


  1. FlawedClassTestBC4 should achieve 100% branch coverage of flawedMethod4 and not reveal the fault therein.

  2. FlawedClassTestSC4 should achieve less than 100% statement coverage of flawedMethod4 and reveal the fault therein.

  3. Both classes should be saved in directory <dir/test.



Decision Table
Add to class FlawedClass the method flawedMethod5 provided here, including the final, commented part (i.e., the table):

v = public boolean flawedMethod5(boolean a, boolean b) {

 int x = -1;

 int y = 2;

 if (a) x = 2;

 else y = 0;

 if (b) y -= x;

 else

  y -= 2;

 return (x / y = 0);

// | a | b |output|

// ================

// | T | T | |

// | T | F | |

// | F | T | |

// | F | F | |

// ================

Coverage required:

Fill in the table in the comments, as follows:


  1. For every possible input, write whether the output is T (true), F (false), or E (division by 0 exceptions)

  2. In the “Coverage required” entry, write the minimal level of coverage that a test suite must achieve to guarantee that the fault in the code is revealed (i.e., to guarantee that a division by zero occurs), among statement, branch, and path coverage. In other words: If every test suite achieving statement coverage reveals the fault, then you should write Coverage required: statement coverage If statement coverage does not guarantee that the fault is revealed, but branch coverage does, you should write Coverage required: branch coverage If neither statement nor branch coverage guarantees that the fault is revealed, but path coverage does, you should write Coverage required: path coverage Finally, if none of these coverage criteria considered guarantees that the fault is revealed, you should write Coverage required: N/A

  3. If the answer is not “N/A”, create a JUnit test class com.work.sample.FlawedClassTestXC5 for class FlawedClass as follows:

  4. FlawedClassTestXC5 should achieve 100% of the coverage named in your comment for flawedMethod5 and reveal the fault therein.

  5. The class should be saved in directory <dir/test.

  6. As usual, commit and push your code to your individual, assigned repository when done and submit the corresponding commit ID on T-Square.


Notes (important-make sure to read carefully):

By “reveal the fault therein”, we mean that the tests which show the division by zero fault should fail with an uncaught exception, so that they are easy to spot.
Do not use compound predicates in your code for the methods of class FlawedClass. That is, only use simple predicates in the form (<operand1 <operator <operand2), such as “if (x 5)” or “while (x = t) ”. In other words, you cannot use logical operators (such as &&, ||) in your predicates.
Your code should compile and run out of the box with a Java version = 1.6
Use JUnit 4 for your JUnit tests.

Powered by