2.1. Annotations
JUnit Jupiter supports the following annotations for configuring tests and extending the framework.
Unless otherwise stated, all core annotations are located in the [org.junit.jupiter.api](https://junit.org/junit5/docs/current/api/org.junit.jupiter.api/org/junit/jupiter/api/package-summary.html)
package in the junit-jupiter-api
module.
Annotation | Description |
---|---|
| Denotes that a method is a test method. Unlike JUnit 4’s |
| Denotes that a method is a parameterized test. Such methods are inherited unless they are overridden. |
| Denotes that a method is a test template for a repeated test. Such methods are inherited unless they are overridden. |
| Denotes that a method is a test factory for dynamic tests. Such methods are inherited unless they are overridden. |
| Denotes that a method is a template for test cases designed to be invoked multiple times depending on the number of invocation contexts returned by the registered providers. Such methods are inherited unless they are overridden. |
| Used to configure the test method execution order for the annotated test class; similar to JUnit 4’s |
| Used to configure the test instance lifecycle for the annotated test class. Such annotations are inherited. |
| Declares a custom display name for the test class or test method. Such annotations are not inherited. |
| Declares a custom display name generator for the test class. Such annotations are inherited. |
| Denotes that the annotated method should be executed before each |
| Denotes that the annotated method should be executed after each |
| Denotes that the annotated method should be executed before all |
| Denotes that the annotated method should be executed after all |
| Denotes that the annotated class is a non-static nested test class. |
| Used to declare tags for filtering tests, either at the class or method level; analogous to test groups in TestNG or Categories in JUnit 4. Such annotations are inherited at the class level but not at the method level. |
| Used to disable a test class or test method; analogous to JUnit 4’s |
| Used to fail a test, test factory, test template, or lifecycle method if its execution exceeds a given duration. Such annotations are inherited. |
| Used to register extensions declaratively. Such annotations are inherited. |
| Used to register extensions programmatically via fields. Such fields are inherited unless they are shadowed. |
| Used to supply a temporary directory via field injection or parameter injection in a lifecycle method or test method; located in the |
Some annotations may currently be experimental. Consult the table in Experimental APIs for details. |
2.1.1. Meta-Annotations and Composed Annotations
JUnit Jupiter annotations can be used as meta-annotations. That means that you can define your own composed annotation that will automatically inherit the semantics of its meta-annotations.
For example, instead of copying and pasting @Tag("fast")
throughout your code base (see Tagging and Filtering), you can create a custom composed annotation named @Fast
as follows. @Fast
can then be used as a drop-in replacement for @Tag("fast")
.
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.junit.jupiter.api.Tag;
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Tag("fast")
public @interface Fast {
}
The following @Test
method demonstrates usage of the @Fast
annotation.
@Fast
@Test
void myFastTest() {
// ...
}
You can even take that one step further by introducing a custom @FastTest
annotation that can be used as a drop-in replacement for @Tag("fast")
and @Test
.
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Tag("fast")
@Test
public @interface FastTest {
}
JUnit automatically recognizes the following as a @Test
method that is tagged with “fast”.
@FastTest
void myFastTest() {
// ...
}