Avatar ·

How to check in JUnit that a method throws a specific exception?

Hi everyone! I just started learning Java and writing unit tests using JUnit 4. I have a method that should throw IllegalArgumentException for invalid input. For example, this method: ```java public int divide(int a, int b) { if (b == 0) { throw new IllegalArgumentException("Divisor cannot be zero"); } return a / b; } ``` I want to write a test that checks that when passing b = 0, this exception is actually thrown. I tried doing this: ```java @Test public void testDivideByZero() { Calculator calc = new Calculator(); calc.divide(10, 0); // expect exception, but test fails with error } ``` But the test just fails with an exception, and JUnit considers it a test error. I heard there is some `expected`