使用JUnit测试异常

时间:2019-04-19 08:56:03

标签: java testing exception junit

测试功能未捕获预期异常作为需求值。它会引发异常,就像一个错误。

我尝试制作

@Rule
    ExpectedException thrown = ExpectedException.none();

    @Test(expected = UnsupportedOperationException.class)
    public void testNotAddingDifferentCurrency(){

        Money money1 = new Money("4.43", Locale.GERMANY);
        Money money2 = new Money("1.436", Locale.US);

        money1.add(money2);
        thrown.expect(InvalidBarcodeException.class);
    }

@Test(expected = UnsupportedOperationException.class)
    public void testNotAddingDifferentCurrency() throws UnsupportedOperationException{

        Money money1 = new Money("4.43", Locale.GERMANY);
        Money money2 = new Money("1.436", Locale.US);

        money1.add(money2);
    }

但是它仍然会给出响应:


java.lang.UnsupportedOperationException
    at main.Money.add(Money.java:44)
    at tests.MoneyTest.testNotAddingDifferentCurrency(MoneyTest.java:44)
    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 junit.framework.TestCase.runTest(TestCase.java:168)
    at junit.framework.TestCase.runBare(TestCase.java:134)
    at junit.framework.TestResult$1.protect(TestResult.java:110)
    at junit.framework.TestResult.runProtected(TestResult.java:128)
    at junit.framework.TestResult.run(TestResult.java:113)
    at junit.framework.TestCase.run(TestCase.java:124)
    at junit.framework.TestSuite.runTest(TestSuite.java:243)
    at junit.framework.TestSuite.run(TestSuite.java:238)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)


Process finished with exit code -1

金钱类主体:

public Money add(Money other) {
        if (!compatibleCurrency(other)) {
            throw new UnsupportedOperationException();
        }
        return new Money(amount.add(other.amount), currency);
    }

    @Override
    public String toString() {
        return decimalFormat.format(amount);
    }

    @Override
    public boolean equals(Object o) {

        Money money = (Money) o;

        if (!amount.equals(money.amount)) return false;
        if (!currency.equals(money.currency)) return false;
        if (!decimalFormat.equals(money.decimalFormat)) return false;

        return true;
    }
}

我期待其他类似主题的答案,但答案看起来像我上面的代码。希望有人知道原因。我希望测试的输出能够通过,但是由于期望,实际输出是处理完成,退出代码为-1。”

1 个答案:

答案 0 :(得分:2)

问题是您将JUnit 3与JUnit 4混合使用。通过在测试类定义中使用extends TestCase,您告诉JUnit该类应作为JUnit 3测试用例运行({{ 1}}是JUnit 3类)。

TestCase@Test批注来自JUnit 4-JUnit 3测试用例的运行者对这些批注一无所知,因此将其注视。

要修复您的测试,只需删除@Rule,一切正常。 JUnit 4至少需要一个extends TestCase注释才能将类解释为测试用例。