应为Java单元测试异常

应为Java单元测试异常,java,android,unit-testing,junit,mocking,Java,Android,Unit Testing,Junit,Mocking,我有一个问题,单元测试应该抛出一个异常 public class MainActivityTest { NumberPicker warmUpSeconds; NumberPicker warmUpMinutes; NumberPicker restSeconds; NumberPicker restMinutes; NumberPicker coolDownSeconds; NumberPicker coolDownMinutes; Nu

我有一个问题,单元测试应该抛出一个异常

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

    }
我不熟悉单元测试,我只是读了一些关于它们的东西,我找不到很多例子。请提前为错误代码道歉

@编辑它甚至没有进入设置时间功能
@edit2解决了,我认为我的模拟没有任何意义,因为我甚至没有将它们传递给函数(函数本身没有获得任何参数),所以我更改了setTimes的定义,使其在int中获得分和秒,在测试中,我只传递了0作为参数。它成功了,异常被抛出

当restTime为0时,根据您的代码引发异常,您的测试模拟restMinutes和restSeconds以返回零,这反过来会使您的restTime为零并引发错误的TimeSetx(Communication),其中Communication为“Communication+=”未设置休息时间”。因此,在测试中进行以下更改,它应该可以正常工作:-

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

解决了,我认为我的模拟没有任何意义,因为我甚至没有将它们传递给函数(函数本身没有任何参数),所以我更改了setTimes的定义,使其在int中获得分秒,在测试中我只传递了0作为参数。它起作用了,引发了异常。

您应该使用调试器逐步检查代码。@OliverCharlesworth Charlesworth好的,我现在检查了它,它甚至没有输入此函数,但我预计会发生此异常,这就是这次测试的重点(至少我有这个意图)
when(restMinutes.getValue()).thenReturn(10);
when(restSeconds.getValue()).thenReturn(15);
when(rounds.getValue()).thenReturn(10);