junit Custom Test Rules


There are benefits for either. Extending ExternalResource it's convenient, especially if we only require a before() to set something up.

However, we should be aware that, since the before() method is executed outside of the try...finally, any code that is required to do clean up in after() won't get executed if there is an error during the execution of before().

This is how it looks inside ExternalResource:

try {
} finally {

Obviously, if any exception is thrown in the test itself, or by another nested rule, the after will still get executed.