0

I am getting 1/4 branch coverage when I run this JUnit test. However, when I change the if statement to "if (points == 1 || points == 2 || points == 3 || points == 4)" it passes the JUnit test. What am I doing wrong?

Main class:

public int getPoints() {
    return points;
}

public Grade(int p) throws IllegalArgumentException {
    if (p < 1 || p > 20)
        throw new IllegalArgumentException();
    points = p;
}

// Your additions/changes below this line

public Classification classify() {

    if (points >= 1 && points <= 4) {
        return Classification.First;
    }
    else {
        throw new IllegalArgumentException("Not a Grade");
    }
}

JUnit Test:

 @Test
 public void testFirst() {
    Assert.assertEquals(Classification.First, new Grade(1).classify());
    Assert.assertEquals(Classification.First, new Grade(2).classify());
    Assert.assertEquals(Classification.First, new Grade(3).classify());
    Assert.assertEquals(Classification.First, new Grade(4).classify());
}
JonathanDavidArndt
  • 2,518
  • 13
  • 37
  • 49
user9044901
  • 3
  • 1
  • 6
  • 1
    If you want to increase your branch coverage, you'll need to add some test cases where the `if` conditions come out `false`. – Dawood ibn Kareem Dec 03 '17 at 00:13
  • Possible duplicate of [eclemma 1 of 2 branch not covered in Junit](https://stackoverflow.com/questions/30226789/eclemma-1-of-2-branch-not-covered-in-junit) – JonathanDavidArndt Dec 03 '17 at 00:18
  • Or maybe even: [Differences between Line and Branch coverage](https://stackoverflow.com/questions/8229236/differences-between-line-and-branch-coverage) – JonathanDavidArndt Dec 03 '17 at 00:20
  • Also, somewhat related: [Branch coverage with JUnit and Mockito](https://stackoverflow.com/questions/37417797/branch-coverage-with-junit-and-mockito) – JonathanDavidArndt Dec 03 '17 at 00:23
  • For more information, see: [Posts containing 'junit branch coverage'](https://stackoverflow.com/search?q=junit+branch+coverage) – JonathanDavidArndt Dec 03 '17 at 00:24

2 Answers2

1

A condition like this has 16 possible branches:

(a == 1 || b == 1 || c == 1 || d == 1)

with all of them false, all of them true, and all in between. The branch checker does not understand that

(points == 1 || points == 2 || points == 3 || points == 4)

has only 5 branches, because it does not analyze the relationship between the conditions.

tkruse
  • 10,222
  • 7
  • 53
  • 80
0

Adding these additional tests to verify edge cases will increase your JUnit coverage to 4/4 for both the constructor and classify methods, at the cost of making a bunch of (possibly) worthless test cases, and also breaking data encapsulation (since you have duplicate validation checks in each of these methods).

@Test(expected = IllegalArgumentException.class)
public void TestSecond()
{
    new Grade(0).classify();
}

@Test(expected = IllegalArgumentException.class)
public void TestThird()
{
    new Grade(5).classify();
}

@Test(expected = IllegalArgumentException.class)
public void TestFourth()
{
    final Grade g = new Grade(1);
    g.points = 0;
    g.classify();
}

@Test(expected = IllegalArgumentException.class)
public void TestFifth()
{
    final Grade g = new Grade(1);
    g.points = 5;
    g.classify();
}
JonathanDavidArndt
  • 2,518
  • 13
  • 37
  • 49