1

I am revising for a test upcoming this week using previous exam questions and I just can't get 100% coverage, my program is :

public int computeInsurance(boolean SportsEquipment, boolean MusicEquipment)
{
int insurance;
if(SportsEquipment == true && MusicEquipment==true)
insurance = 20;
else if((SportsEquipment == true && MusicEquipment == false)||(SportsEquipment == false        && MusicEquipment == true))
        insurance = 10;
    else
        insurance= 5;

    return insurance;
}

}

I am using the following test cases:

public class Lab5CarTest {

@Test
public void testComputeInsurance() {
    Lab5Car t = new Lab5Car();

    int result = t.computeInsurance(true, true);
    assertEquals(20,result);

    int i = t.computeInsurance(true,false);
    assertEquals(10,i);

    int u = t.computeInsurance(false,false);
    assertEquals(5,u);


}

}

But I am missing 3 branches out of 8 in the else if line of my code!

3
  • Where's the false, true case ? Commented Nov 11, 2012 at 18:34
  • add "int i = t.computeInsurance(false,true);" and see what you get Commented Nov 11, 2012 at 18:35
  • Ahh i should have seen that one! But still after I add that line, I still am missing 2 branches out of 8 from my else if line of code! Commented Nov 11, 2012 at 19:36

2 Answers 2

3

Why don't you test for:

int ip = t.computeInsurance(false,true);
assertEquals(10,ip);

Like this you should cover all.

you can simplify your code like this:

if (SportsEquipment && MusicEquipment)
    insurance = 20;
else if (SportsEquipment != MusicEquipment)
    insurance = 10;
else
    insurance= 5;
Sign up to request clarification or add additional context in comments.

2 Comments

Ahh i should have seen that one! But still after I add that line, I still am missing 2 branches out of 8 from my else if line of code!
Try updating your if tree like Joop did show below. I did copy it to my answer to be complete.
0

1 out of 4 (=2²) you are missing (not 2³), t.computeInsurance(false, true);

S M  I
------
t t 20
t f 10
f t 10
f f  5


if (SportsEquipment  && MusicEquipment)
    insurance = 20;
else if (SportsEquipment != MusicEquipment)
    insurance = 10;
else
    insurance= 5;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.