Junit eclipse中的错误测试异常

Error test exception in Junit eclipse

当我尝试测试异常时,我一直收到错误(不是失败)。还尝试了@rule。同样的事情发生了。

public Money addSameCurrency(Money other) {
    if (!currency.equals(other.currency)) {
        throw new IllegalArgumentException();
    }

    Money m = new Money(amount + other.amount, currency);
    return m;
}

@Test(expected = IllegalArgumentException.class)
public void testExceptionIsThrown() {
    Money m1 = new Money(10, "USD");
    Money m2 = new Money(15, "EUR");
    Money sum = m1.addSameCurrency(m2);
}

我猜测您的测试直接或间接扩展了 junit.frameworkTestCase。如果是这样,那可能会使测试 运行 成为 JUnit3 样式的测试(因此 @Test 注释将被忽略)