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

1

17

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");
    ...
}
Skulduggery answered 13/6, 2020 at 4:12 Comment(0)
K
21

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:

Kuntz answered 13/6, 2020 at 8:16 Comment(2)
Works great, thanks! I was so engrossed in looking for a workaround for this that I did not read the workings of Assert.assertThrows, the best being the flexibility to choose assertion framework and make assertions on the caught exception.Skulduggery
The problem in my opinion is that the deprecation is done on the method none() but surely should be done on the class ExpectedException? I was looking for an alternative method of using ExpectedException but that's not the migration strategy.Benue

© 2022 - 2024 — McMap. All rights reserved.