devlperMoose
devlperMoose

Reputation: 315

Catch thrown exception in Junit without Throwable block swallowing it

I have a class that divides two numbers. When a number is divided by 0, it throws ArithmeticException. But when I unit test this, on console it is showing that ArithmeticException is thrown, but my test is failing with an AssertionError. I want to know if there is a way to prove that it is throwing ArithmeticException in Junit?
Example.java

public class Example {

public static void main(String[] args)
{
    Example ex = new Example();
    ex.divide(10, 0);
}

public String divide(int a, int b){
    int x = 0;
    try{
        x = a/b;
    }
    catch(ArithmeticException e){
        System.out.println("Caught Arithmetic Exception!");
    }
    catch(Throwable t){
        System.out.println("Caught a Different Exception!");
    }
    return "Result: "+x;
}
}

ExampleTest.java

public class ExampleTest {
    @Test(expected=ArithmeticException.class)
    public void divideTest()
    {
        Example ex = new Example();
        ex.divide(10, 0);
    }
}

My actual code is different, since that has lot of dependencies, I simpflied my requirement to this example test. Please suggest.

Upvotes: 0

Views: 664

Answers (4)

Peter Lawrey
Peter Lawrey

Reputation: 533820

divide isn't throwing this exception.

Your options are

  • extract the inner portion of the try/catch as a method you can call from the unit tests.
  • capture the System.err in the unit test and check it tries to prints the error you expect.

You can extract the method using your IDE like this

public static String divide(int a, int b){
    int x = 0;
    try{
        x = divide0(a, b);
    }
    catch(ArithmeticException e){
        System.out.println("Caught Arithmetic Exception!");
    }
    catch(Throwable t){
        System.out.println("Caught a Different Exception!");
    }
    return "Result: "+x;
}

static int divide0(int a, int b) {
    return a/b;
}

@Test(expected = ArithmeticException.class)
public void testDivideByZero() {
    divide0(1, 0);
}

Upvotes: 2

Laj
Laj

Reputation: 11

Your divide() method is catching the ArithmeticException but not doing anything with it (other than printing to console that it was caught). If the divide() method is supposed to throw the ArithmeticException, then you have two choices:

  • Remove the try/catch within the divide() method. As soon as you attempt to divide by 0, it will automatically throw an ArithmeticException, and your test case will pass upon receiving the expected Exception class.
  • Or, after printing to console that an ArithmeticException was caught, throw that exception back up to the calling method.

Upvotes: 1

dbrown93
dbrown93

Reputation: 344

JUnit isn't catching the exception because you've already caught it in your method. If you remove the try catch block in "divide", JUnit will catch the arithmetic exception and your test will pass

Upvotes: 1

rgettman
rgettman

Reputation: 178323

You're getting the AssertionError because the expected exception, ArithmeticException, didn't get thrown by the test method. You need to let the ArithmeticException propagate out of the method to be tested, divide. Don't catch it. Don't catch anything in divide.

Upvotes: 1

Related Questions