Java 使用JUnit测试异常

Java 使用JUnit测试异常,java,testing,exception,junit,Java,Testing,Exception,Junit,测试函数未将预期异常捕获为所需值。它像抛出错误一样抛出异常 我试着做了 @Rule ExpectedException thrown = ExpectedException.none(); @Test(expected = UnsupportedOperationException.class) public void testNotAddingDifferentCurrency(){ Money money1 = new Money("4.43",

测试函数未将预期异常捕获为所需值。它像抛出错误一样抛出异常

我试着做了

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

但还是有人回应:


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。”

问题是您将JUnit3与JUnit4混合在一起。通过在测试类定义中使用
extenstedtestcase
,您告诉JUnit该类应该作为JUnit3测试用例运行(
TestCase
是JUnit3类)

@Test
@Rule
注释来自JUnit 4-JUnit 3测试用例的运行者对这些注释一无所知,因此它将它们删除


要修复您的测试,只需删除
扩展测试用例
,一切都应该正常。至少有一个
@Test
注释的存在是junit4将类解释为测试用例所需要的全部?您是如何注释测试类的?RunWith?junit 4.10和
公共类MoneyTest扩展了TestCase
必须有一个@RunWith,你想用你的规则实现什么?在JUnit4中,将预期的异常添加到
@Test
注释就足够了。您的主类主体不完整。stacktrace显示您正在使用JUnit38ClassRunner,因此我认为这使您的运行方式与JUnit3相同。因为您没有显示完整的testclass,所以我们无法理解为什么要使用这个运行程序。
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;
    }
}