Software Engineering Glossary

JUnit Rule

Also known as: @Rule TestRule JUnit 4 Rule MethodRule

A JUnit Rule is a reusable object that wraps a JUnit 4 test method (or a whole class with @ClassRule) so setup, cleanup, timeouts, and extra checks live in one place. You implement TestRule or extend ExternalResource, then put a public field annotated with @Rule on each test class that needs it. That is the same job as setUp and tearDown, without copying those methods into every class. JUnit 5 and 6 replace Rules with extensions.

Key Takeaways

  • A Rule wraps the test Statement: work before evaluate() is setup, work in finally after it is cleanup.
  • The @Rule field must be public and non-static. @ClassRule fields must be public and static.
  • Write a Rule when more than one test class needs the same lifecycle. Keep @Before/@After for logic that is unique to one class.
  • On new Jupiter tests, prefer an Extension instead of adding more TestRule classes.

How It Works

  1. The JUnit 4 runner finds @Rule with reflection, the same way it finds @Test.
  2. apply receives the current test Statement and returns a wrapper Statement.
  3. The wrapper runs around @Before, the test method, and @After.
  4. Several rules nest. RuleChain or @Rule(order = ...) makes that order explicit.

Where It Is Used

  • Teams extract a DatabaseResetRule so every service test starts from an empty schema.
  • JUnit ships TemporaryFolder, Timeout, ExpectedException, and ExternalResource.
  • Older Android Espresso tests use ActivityTestRule, which is a Rule that launches an Activity.

Related glossary terms