Junit rules allow you to write code to do some before and after work. Thus, you don’t repeat to write 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 webpage.

  • 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 is 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. This 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 to have more control on 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.

Output:

Verifier Rule

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

Output:

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.

Output:

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 server before test and stop it after test, we should use ClassRule annotation. ClassRule must annotates public static field.

MyServer.java

ServerTest.java

TestFirstServer.java

TestSecondServer.java

RuleChain Rule

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

LoggingRule.java

RuleChainTest.java

Output:

Custom Rules

In order to write your custom rule, we need to impelemt the TestRule interface. This interface’s only method that called apply(Statement, Description) returns an instance of Statement. Statement represents our tests within the Junit runtime and Statement#Evaluate() executes them. 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

Output:

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.

ScreenShotRule.java

ScreenshotRuleTest.java

Summary

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

Keywords

JUnit rules, unit testing, test framework