我有问题,单元测试应该抛出异常
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和其他&#34; Times&#34;是包私有的,我在测试期间得到的错误是:
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)
当你根据你的代码&amp;和restTime为0时抛出异常你的测试嘲笑restMinutes&amp; restSeconds返回零,这反过来使你的restTime为零&amp;抛出WrongTimeSetEx(沟通)沟通是&#34;沟通+ =&#34;没有时间设置为休息时间&#34;。因此,在您的测试中进行以下更改:它应该工作正常: -
when(restMinutes.getValue()).thenReturn(10);
when(restSeconds.getValue()).thenReturn(15);
when(rounds.getValue()).thenReturn(10);
答案 1 :(得分:0)
解决了,我认为我的模拟没有任何意义,因为我甚至没有将它们传递给函数(函数本身没有得到任何参数),所以我改变了setTimes的定义和使它在int中得到分钟和秒,在测试中我只传递0作为参数。它起作用,抛出异常。