Alternatives to the deprecated ExpectedException.none() in JUnit 4.13

23.6k Views Asked by At

I am trying to use the @Rule annotation with ExpectedException

The ExceptedException.none() method to initialize a variable type of ExceptedException says it has been deprecated what are the alternatives to initialize an object of the ExceptedException. Example:

public ExpectedException expectedException = ExpectedException.none();

@Test
public void testThrownException() throws Exception {
    expectedException.expect(CustomException.class);
    expectedException.expectMessage("Exception Message");
    ...
}
1

There are 1 best solutions below

2
Joe On BEST ANSWER

Did you read the deprecation notice?

Deprecated. Since 4.13 Assert.assertThrows can be used to verify that your code throws a specific exception.

See this answer for an example: