2017-04-17 83 views
0

我有一個問題,單元測試應該拋出一個異常Java單元測試例外預期

public class MainActivityTest { 
    NumberPicker warmUpSeconds; 
    NumberPicker warmUpMinutes; 
    NumberPicker restSeconds; 
    NumberPicker restMinutes; 
    NumberPicker coolDownSeconds; 
    NumberPicker coolDownMinutes; 
    NumberPicker workMinutes; 
    NumberPicker workSeconds; 
    NumberPicker rounds; 
    @Test(expected = WrongTimeSetEx.class) 
    public void testButtonOnClick() throws WrongTimeSetEx { 
     MainActivity tested=mock(MainActivity.class); 
     warmUpSeconds=mock(NumberPicker.class); 
     warmUpMinutes=mock(NumberPicker.class); 
     restSeconds=mock(NumberPicker.class); 
     restMinutes=mock(NumberPicker.class); 
     coolDownMinutes=mock(NumberPicker.class); 
     coolDownSeconds=mock(NumberPicker.class); 
     workMinutes=mock(NumberPicker.class); 
     workSeconds=mock(NumberPicker.class); 
     rounds=mock(NumberPicker.class); 
     when(warmUpSeconds.getValue()).thenReturn(0); 
     when(warmUpMinutes.getValue()).thenReturn(0); 
     when(restMinutes.getValue()).thenReturn(0); 
     when(restSeconds.getValue()).thenReturn(0); 
     when(coolDownSeconds.getValue()).thenReturn(10); 
     when(coolDownMinutes.getValue()).thenReturn(15); 
     when(workMinutes.getValue()).thenReturn(10); 
     when(workSeconds.getValue()).thenReturn(10); 
     when(rounds.getValue()).thenReturn(0); 
     tested.setTimes(); 
    } 
} 

,並有檢驗方法

public void setTimes() throws WrongTimeSetEx 
    { 
     warmUpTime = warmUpSeconds.getValue(); 
     warmUpTime += 60 * warmUpMinutes.getValue(); 
     restTime = restSeconds.getValue(); 
     restTime += 60 * restMinutes.getValue(); 
     coolDownTime = coolDownSeconds.getValue(); 
     coolDownTime += 60 * coolDownMinutes.getValue(); 
     workTime = workSeconds.getValue(); 
     workTime += 60 * workMinutes.getValue(); 
     roundsNumber = rounds.getValue(); 
     String communicate=""; 
     if(restTime==0) //check if times are correct and make sense 
      communicate+="No time set for Rest Time!<br>\n"; 
     if(workTime==0) 
      communicate+="No time set for Work Time!<br>\n"; 
     if(roundsNumber==0) 
      communicate+="No rounds number is set!<br>\n"; 
     if(!communicate.isEmpty()) 
      throw new WrongTimeSetEx(communicate); 

    } 

warmUpTimes和「泰晤士報」的,其餘都是包私有,我在測試期間得到的錯誤是:

java.lang.AssertionError: Expected exception: jablonskijakub.intervaltraining.exceptions.WrongTimeSetEx 

    at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:32) 
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137) 
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69) 
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234) 
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:498) 
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) 


Process finished with exit code -1 

我對單元測試不熟悉,我剛剛讀了一些東西他們,我找不到很多例子。對不起,代碼提前抱歉。

@edit它甚至沒有輸入setTimes函數 @ edit2解決了,我認爲我的嘲笑沒有任何意義,因爲我甚至沒有把它們傳遞給函數(函數本身沒有得到任何參數),所以我改變了setTimes的定義,並讓它在int中獲得分鐘和秒,並且在測試中,我只傳遞了0作爲參數。它的工作,異常被拋出。

+0

您應該使用調試器逐步執行代碼。 –

+0

@OliverCharlesworth Charlesworth好吧,我現在檢查了它,它甚至沒有輸入這個函數 –

回答

0

解決,我認爲我的嘲笑沒有任何意義,因爲我甚至不通過他們發揮作用(函數本身沒有得到任何參數),所以我改變了setTimes的定義並使其得到分鐘和秒在整數和測試中,我只傳遞了0作爲參數。它的工作,異常被拋出。

0

當你在投擲異常時restTime根據自己的代碼是0 &測試嘲笑restMinutes & restSeconds返回零,這又使得您restTime零&拋出WrongTimeSetEx(通信),其中通信是「通信+ =」否時間集。休息時間」因此,請在您的測試以下變化&它應該很好地工作: -

when(restMinutes.getValue()).thenReturn(10); 
when(restSeconds.getValue()).thenReturn(15); 
when(rounds.getValue()).thenReturn(10); 
+0

但我期待這種異常發生,這就是這個測試的要點(至少我有這個意圖) –