【问题标题】:Java unit test exception expected预期 Java 单元测试异常
【发布时间】:2017-04-17 19:17:03
【问题描述】:

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

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 和“Times”的其余部分是包私有的,我在测试期间得到的错误是:

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 和 test 中,我只传递了 0 作为参数。它起作用了,抛出了异常。

【问题讨论】:

  • 您应该使用调试器单步调试您的代码。
  • @OliverCharlesworth Charlesworth 嗯,我现在查了一下,竟然没有进入这个函数

标签: java android unit-testing junit mocking


【解决方案1】:

当您根据您的代码在 restTime 为 0 时抛出异常,并且您的测试模拟 restMinutes 和 restSeconds 返回零,这反过来使您的 restTime 为零并抛出 WrongTimeSetEx(communicate),其中通信为“communicate+=”没有时间设置休息时间”。因此,在您的测试中进行以下更改,它应该可以正常工作:-

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

【讨论】:

  • 但我预计会发生这种异常,这就是这次测试的重点(至少我有这个意图)
【解决方案2】:

已解决,我认为我的模拟没有任何意义,因为我什至没有将它们传递给函数(函数本身没有获取任何参数),所以我更改了 setTimes 的定义并使其获取分钟和在 int 和 test 中的秒数我只传递了 0 作为参数。成功了,抛出异常。

【讨论】:

    猜你喜欢
    • 2021-07-29
    • 2019-01-23
    • 1970-01-01
    • 2019-04-11
    • 1970-01-01
    • 2012-04-05
    • 2015-08-09
    • 2011-07-31
    • 2013-10-08
    相关资源
    最近更新 更多