2012-11-11 107 views
1

我修改了測試使用以前的考題,我就不能得到100%的覆蓋率即將舉行的這一週,我的計劃是:Java軟件測試

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; 
} 

} 

我使用下面的測試情況:

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); 


} 

} 

但是我錯過了3個分支中的8個,其他的如果我的代碼行!

+0

在哪裏'假,TRUE'情況? –

+0

add「int i = t.computeInsurance(false,true);」並看看你得到了什麼 – bobah

+0

啊,我應該看到那個!但是,在我添加該行後,仍然缺少8行中的2個分支,如果其中有一行代碼! – user1816464

回答

3

你爲什麼不測試:

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

這樣你就應該涵蓋所有。

可以簡化你這樣的代碼:

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

啊,我應該看到那個!但是,在我添加該行後,仍然缺少8行中的2個分支,如果其中有一行代碼! – user1816464

+0

嘗試更新您的如果樹像喬普確實顯示在下面。我把它複製到我的答案是完整的。 – Frank

0

1出4(= 2 2)你缺少(不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;