In your example, assertEquals will compare the return value of the method call with the expected value, which you don't want, and of course, there will be no return value if the expected exception is expected. Move assertEquals to the catch block:
@Test public void testTC3() { try { Shipping.shippingCost('P', -5); fail(); // if we got here, no exception was thrown, which is bad } catch (Exception e) { final String expected = "Legal Values: Package Type must be P or R"; assertEquals( expected, e.getMessage()); } }
source share