JUnit
lastmod 2024-11-28created 2014-06-0513 edits
Install
To avoid interoperability issues, use Maven Surefire/Failsafe 3.0.0-M4 or later.
Maven
Unless you use Spring Boot, which defines its own way of managing dependencies, it is recommended to use the JUnit Platform BOM to align the versions of all JUnit 5 artifacts.
The following is <dependencyManagement>, not <dependencies>.
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.11.3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Configuring test engines
For Maven Surefire or Maven Failsafe to run any tests, at least one TestEngine implementation must be added to the test classpath.
To configure support for JUnit Jupiter based tests, configure test scoped dependencies on the JUnit Jupiter API and the JUnit Jupiter TestEngine implementation similar to the following.
<!-- ... -->
<dependencies>
<!-- ... -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.11.3</version> <!-- can be omitted when using the BOM -->
<scope>test</scope>
</dependency>
<!-- ... -->
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.3.1</version>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.3.1</version>
</plugin>
</plugins>
</build>
<!-- ... -->
Maven Surefire and Maven Failsafe can run JUnit 4 based tests alongside Jupiter tests as long as you configure test scoped dependencies on JUnit 4 and the JUnit Vintage TestEngine implementation similar to the following.
<!-- ... -->
<dependencies>
<!-- ... -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.11.3</version> <!-- can be omitted when using the BOM -->
<scope>test</scope>
</dependency>
<!-- ... -->
</dependencies>
<!-- ... -->
<build>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.3.1</version>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.3.1</version>
</plugin>
</plugins>
</build>
<!-- ... -->
Simple example
import static org.junit.jupiter.api.Assertions.assertEquals;
import example.util.Calculator;
import org.junit.jupiter.api.Test;
class MyFirstJUnitJupiterTests {
private final Calculator calculator = new Calculator();
@Test
void addition() {
assertEquals(2, calculator.add(1, 1));
}
}
JUnit 5
Unlike previous versions, JUnit 5 consists of three sub-project modules.
- JUnit Platform : includes the TestEngine API, Console Launcher, JUnit 4 based Runner, etc.
- JUnit Jupiter : implements JUnit 5 as a TestEngine API implementation
- JUnit Vintage : implements JUnit 3 and 4 as a TestEngine API implementation
Because JUnit 5 includes the JUnit Vintage module, you can use JUnit 3 and 4 syntax. However, it is not fully supported, so extra work is needed if you use it.
Functions
Basic test functions:
-
assertEquals(expected, actual)
Annotations
What is the purpose of @SmallTest, @MediumTest, and @LargeTest annotations.
This blog post explains it best. Basically, it is the following:
- Small: this test doesn't interact with any file system or network.
- Medium: Accesses file systems on box which is running tests.
- Large: Accesses external file systems, networks, etc.
| Feature | Small | Medium | Large |
| Network access | No | localhost only | Yes |
| Database | No | Yes | Yes |
| File system access | No | Yes | Yes |
| Use external systems | No | Discouraged | Yes |
| Multiple threads | No | Yes | Yes |
| Sleep statements | No | Yes | Yes |
| System properties | No | Yes | Yes |
| Time limit (seconds) | 60 | 300 | 900+ |
Setting up JUnit in intellij and writing simple test code
See also
- Android:JUnit: description of JUnit on Android.
- AssertJ