O'Reilly logo

Mastering Software Testing with JUnit 5 by Boni García

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

JUnit 4 rules

Due to the strict limitation of uniqueness of a JUnit 4 runner within the same test class, version 4.7 of JUnit introduced the concept of method-level rules, which are annotated fields in a test class with @Rule. These rules allow for addition or redefinition of test behavior by executing some code before and after the execution of the test. JUnit 4.9 also incorporates the concept of class-level rules, which are rules that are executed before and after all tests within the class. These rules are identified by annotating static fields with @ClassRule, as shown in the following example:

import org.junit.ClassRule;import org.junit.Test;import org.junit.rules.TemporaryFolder;public class MyRuleTest {   @ClassRule   public static TemporaryFolder ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required