Тестовая функция не перехватывает ожидаемое исключение как требуемое значение. Выдает исключение как ошибку.
Я пытался сделать
@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 в случае Ожидания. ”