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