Introduction to JUnit 4 Rules
After we have finished this lesson, we
- Know what a JUnit 4 rule is.
- Can use JUnit 4 rules.
- Can create files and directories which are deleted after a test method has been run.
Get the source code from Github.
What Is a JUnit 4 Rule?
A JUnit 4 rule is a component that intercepts test method calls and allows us to do something before a test method is run and after a test method has been run. All JUnit 4 rule classes must implement the org.junit.rules.TestRule
interface.
We can do pretty cool stuff with JUnit 4 rules. For example, JUnit 4 provides rules that can:
- Create files and directories which are deleted after a test method has been run.
- Fail a test if the specified timeout is exceeded before a test method has been run.
- Configure an external resource such as a socket or a database connection before a test method is run and free the external resource after a test method has been run.
Next, we will find out how we can use JUnit 4 rules in our test classes.
Using Junit 4 Rules
We can use JUnit 4 rules by following these steps:
- Add a
public
field to our test class and ensure that the type of this field is a subtype ofTestRule
. - Annotate the field with the
@Rule
annotation.
JUnit 4 requires that rule fields are public
, aren’t static
, and are a subtype of TestRule
.
Let’s assume that we want to use the FooBarRule
in our test class. After we have enabled this rule, the source code of our test class looks as follows:
import org.junit.Rule; public class RuleTest { @Rule public FooBarRule rule = new FooBarRule(); }
Additional Reading:
Let’s use this information and find out how we can create files and directories which are deleted after a test method has been run.
Using the TemporaryFolder Rule
If we have to create files and/or directories which are deleted after a test method has been run, we have to use the TemporaryFolder
rule. As we remember, if we want to enable this rule, we have to add a public TemporaryFolder
field to our test class and annotate that field with the @Rule
annotation.
After we have enabled the TemporaryFolder
rule, the source code of our test class looks as follows:
import org.junit.Rule; import org.junit.rules.TemporaryFolder; public class RuleTest { @Rule public TemporaryFolder tmpFolder = new TemporaryFolder(); }
We can now create files and directories by using the TemporaryFolder
rule. Let’s create a simple test that creates one file which is deleted after our test method has been run.
In other words, we have to add a new test method to our test class and implement this method by following these steps:
- Create a new file.txt file.
- Ensure that the returned
File
object is a file. - Ensure that the file was created to the root directory of the temporary folder.
After we have implemented this test method, the source code of our test class looks as follows:
import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; import java.io.File; import java.io.IOException; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; public class RuleTest { @Rule public TemporaryFolder tmpFolder = new TemporaryFolder(); @Test public void shouldCreateNewFileInTemporaryFolder() throws IOException { File created = tmpFolder.newFile("file.txt"); assertTrue(created.isFile()); assertEquals(tmpFolder.getRoot(), created.getParentFile()); } }
The TemporaryFolder
rule doesn’t ensure that the created files and directories were deleted successfully. If we are using JUnit 4.13 or newer, we can configure the TemporaryFolder
rule to throw an AssertionError
if the created files and directories cannot be deleted.
If we want to enable this feature, we have to create the TemporaryFolder
object by using the following code:
[code lang=”java”]
@Rule
public TemporaryFolder folder = TemporaryFolder.builder()
.assureDeletion()
.build();
[/code]
Additional Reading:
Let’s summarize what we learned from this lesson.
Summary
This lesson has taught us four things:
- A JUnit 4 rule is a component that intercepts test method calls, and allows us to do something before a test method is run and after a test method has been run.
- JUnit 4 requires that rule fields are annotated with the
@Rule
annotation. - JUnit 4 requires that rule fields are
public
, aren’tstatic
, and are a subtype ofTestRule
. - The
TemporaryFolder
rule allows us to create files and/or directories which are deleted after a test method has been run.
Previous LessonNext Topic