2017-04-17 81 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

但我期待这种异常发生,这就是这个测试的要点(至少我有这个意图) –