vendredi 19 avril 2019

Test Exception using JUnit

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

Aucun commentaire:

Enregistrer un commentaire