junit Custom Test Rules Custom @TestRule by extension

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

JUnit has an abstract implementation of @TestRule that lets you write a rule in a more simpler way. This is called ExternalResource and provides two protected methods that can be extended like this:

public class AwesomeTestRule extends ExternalResource {

    @Override
    protected void before() {
        // do your magic
    }

    @Override
    protected void after() {
        // undo your magic, if needed
    }

}



Got any junit Question?