Reputation: 223
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!
Upvotes: 1
Views: 228
Reputation: 15661
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;
Upvotes: 3
Reputation: 109613
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;
Upvotes: 0