Writing JUnit Rules



Description of rules

Rules allow very flexible addition or redefinition of the behavior of each test method in a test class. Testers can reuse or extend one of the provided Rules below, or write their own.

This mean, that you can controll the behavior of your test case, you can catch exceptions, write before-after methods, etc.

I would recommend to read more about rules here https://github.com/junit-team/junit/wiki/Rules

@Rule vs @ClassRule

There is a difference between @Rule and @ClassRule in JUnit.We can differ them similiar as @Before and @BeforeClass. If you will annotate field ,that implements org.junit.rules.TestRule with @Rule annotation, then it will run in each method. But, if you will annotate this field as @ClassRule, then it will run once.

Useful rules examples
Writing your own rule

We will write custom TestRule, that will log actions. We will have two

We will get

before: global
before: local
after: local
after: global