Test function doesn't catch expected exception as demanded value. It throws exception like an error one.
I tried making
@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);
}
and
@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);
}
But it's still give response :
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
Money class body :
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;
}
}
I was looking forward answer on other similar topics but the answers looks like my code above. Hope someone know the reason. I expect the output of test to be passed, but the actual output is Process finished with exit code -1 in cause of Expection.”
The problem is that you're mixing JUnit 3 with JUnit 4. By using extends TestCase
in your test class definition, you're telling JUnit that the class should be run as a JUnit 3 test case ( TestCase
is a JUnit 3 class).
The @Test
and @Rule
annotations are from JUnit 4 - the runner for JUnit 3 test cases doesn't know anything about these annotations, so it ginores them.
To fix your test, just remove the extends TestCase
, and everything should work. The presence of at least one @Test
annotation is all JUnit 4 needs to interpret a class as a test case.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.