Use MethodRule as a common solution, if you have many test cases to test
public class ExceptionRule implements MethodRule {
@Override
public Statement apply(final Statement base, final FrameworkMethod method, Object target) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
try {
base.evaluate();
Assert.fail();
} catch (CriticalServerException e) {
//Analyze the exception here
}
}
};
}
}
Then use the Rule to your test class:
@Rule public ExceptionRule rule = new ExceptionRule();