I am trying to test the functionality of dividing by zero.
If I just do: System.out.println(5/0)
, I will get java.lang.ArithmeticException
I am trying to test that the exception was thrown.
Here's my unit test:
@Test(expected = ArithmeticException.class)
public void given_divideByZeroInput_when_divideByZero_then_verify_throwing_exception() {
MathClass sut = new MathClass();
sut.div(10,0);
}
and my div
is this simple:
public float div(int x, int y) {
return (float) x/y;
}
However, the unit test is failing, stating:
java.lang.AssertionError: Expected exception: java.lang.ArithmeticException
What did I do wrong?
I know there are many ways to test the thrown exception, but I am trying to stick with the simple @Test(excepted = .. )