JUnit rules allow you to write code to do some before and after work. Thus, you don’t repeat to write the same code in various test classes. They are very useful to add more functionalities to all test methods in a test class. You can extend or reuse provided rules or write your own custom rules. The base rules are listed below and you can find details about them on https://github.com/junit-team/junit/wiki/Rules web page.

  • TemporaryFolder Rule
  • ExternalResource Rules
  • ErrorCollector Rule
  • Verifier Rule
  • TestWatchman/TestWatcher Rules
  • TestName Rule
  • Timeout Rule
  • ExpectedException Rules
  • ClassRule
  • RuleChain
  • Custom Rules

In Junit github link you can find all details of each rule. Also, their core mechanism and flow are described in this article very well. I want to demonstrate usage of some rules with examples.

Temporary Folder

The TemporaryFolder Rule allows you to create files and folders. These files are folders are deleted whether the test passes or fails when the test method finishes. By default, no exception is thrown if resources cannot be deleted. So, if you need to run a test that needs a temporary file or folder then you can use this rule as shown below.

Timeout Rule

The Timeout Rule applies the same timeout to all test methods in a class.

ExpectedException Rules

The ExpectedException Rule allows having more control over of expected exception types and messages.

Error Collector Rule

The ErrorCollector Rule allows execution of a test to continue after the first problem is found. It collects all the errors and reports them all at once.


Verifier Rule

 Verifier rule does a verification check and if it is failed, the test is finished with failing result. You can write your custom verification logic with Verifier Rule.



TestWatchman/TestWatcher Rules

TestWatcher (and the deprecated TestWatchman) are classes for Rules and they watch test methods and write log for the each passing and failing tests. You can keep an eye on the tests with TestWatcher Rule.


TestName Rule

 The TestName Rule allows you to get current test name inside the test method.

ExternalResources & Class Rule

ExternalResource is a base class for Rules (like TemporaryFolder) that establishes an external resource before a test (a file, socket, server, database connection, etc.), and guarantee to tear it down afterward. This rule is more useful for integration tests.

With ClassRule annotation we can extend ExternalResource operation to multiple classes. It is very useful for when we need to repeat test setup/teardown for multiple classes. For example, if we are doing an integration test and we have to start the server before the test and stop it after the test, we should use ClassRule annotation. ClassRule must annotate public static field.





RuleChain Rule

With JUnit 4.10, we can order several rules according to our needs using RuleChain rule.




Custom Rules

In order to write your custom rule, we need to implement the TestRule interface. This interface’s only method that called apply(Statement, Description) returns an instance of Statement. The statement represents our tests within the Junit runtime and Statement#Evaluate() executes them. The description describes the individual test.

“The power from implementing TestRule comes from using a combination of custom constructors, adding methods to the class for use in tests, and wrapping the provided Statement in a new Statement.” (https://github.com/junit-team/junit/wiki/Rules)

Custom Rules can be written simply as follows:

We can use them in our tests by using @Rule annotation


I also want to give a real-life example. The following example is taking a screenshot when our web automation test fails. In below example, webdriver cannot find specified id and throws an exception, then it will be caught in evaluate() function’s catch block and it takes a screenshot.



Summary of JUnit Rules

  • You learned how to use and the meanings of JUnit Rules.
  • You practiced with examples of  JUnit Rules.


JUnit rules, unit testing, test framework