2

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

5
  • this is junit4? how did you annotate the test class? RunWith ? Commented Apr 19, 2019 at 9:01
  • junit 4.10 and public class MoneyTest extends TestCase Commented Apr 19, 2019 at 9:11
  • there must be a @Runwith there somewhere Commented Apr 19, 2019 at 9:12
  • What do you want to achive with your rule? In JUnit 4 it is sufficient to add the expected exception to the @Test annotation. And your main class body is incomplete. Commented Apr 19, 2019 at 9:40
  • Your stacktrace shows that you are using JUnit38ClassRunner, so I think this makes you run same as JUnit3. Since you do not show the full testclass, we cannot see why this runner is used. Commented Apr 19, 2019 at 10:02

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.