Sub topic 17
Coming from JUnit 4
Many Java projects still have tests written with JUnit 4. JUnit 4 is the older version of JUnit, first released in 2006. This chapter shows the main differences between JUnit 4 and JUnit 5, so you can read old tests and move them to the new style.
Why the Two Versions Look Different
JUnit 5 was rebuilt from scratch. Its parts were split into modules:
- JUnit Platform is the base that launches tests and reports results to your IDE or build tool.
- JUnit Jupiter is the new way to write tests. It is what you have used in this tutorial.
- JUnit Vintage is an engine. An engine is a part that finds and runs one kind of test. The Vintage engine runs old JUnit 4 tests on the new platform.
Because the Vintage engine exists, you don't have to change every test at once.
The Main Changes at a Glance
| JUnit 4 | JUnit 5 |
|---|---|
| `org.junit.Test` | `org.junit.jupiter.api.Test` |
| `@Before` / `@After` | `@BeforeEach` / `@AfterEach` |
| `@BeforeClass` / `@AfterClass` | `@BeforeAll` / `@AfterAll` |
| `@Ignore` | `@Disabled` |
| `@Category` | `@Tag` |
| `@RunWith` and `@Rule` | `@ExtendWith` |
| `@Test(expected = ...)` | `assertThrows(...)` |
| `@Test(timeout = ...)` | `@Timeout` or `assertTimeout` |
| Message is the **first** assertion argument | Message is the **last** assertion argument |
| Classes and methods must be `public` | `public` is not needed |
Before and After: One Test, Two Styles
Here is a JUnit 4 test for the Calculator class from earlier chapters:
import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.Test;
public class CalculatorJUnit4Test {
private Calculator calculator;
@Before
public void setUp() {
calculator = new Calculator();
}
@Test
public void addReturnsSum() {
assertEquals("2 + 3 should be 5", 5, calculator.add(2, 3));
}
@Test(expected = IllegalArgumentException.class)
public void divideByZeroThrows() {
calculator.divide(10, 0);
}
}Here is the same test in JUnit 5:
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class CalculatorJUnit5Test {
private Calculator calculator;
@BeforeEach
void setUp() {
calculator = new Calculator();
}
@Test
void addReturnsSum() {
assertEquals(5, calculator.add(2, 3), "2 + 3 should be 5");
}
@Test
void divideByZeroThrows() {
IllegalArgumentException ex = assertThrows(
IllegalArgumentException.class,
() -> calculator.divide(10, 0));
assertEquals("divisor must not be zero", ex.getMessage());
}
}Notice three things:
- The imports come from
org.junit.jupiter.api. - The message
"2 + 3 should be 5"moved to the end. assertThrowsgives back the exception, so you can also check its message. JUnit 4'sexpectedcould not do that.
Running Old Tests While You Migrate
To keep JUnit 4 tests running next to new ones, add the Vintage engine in Maven:
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.11.3</version>
<scope>test</scope>
</dependency>Then move one test class at a time. When no JUnit 4 tests are left, remove the Vintage engine and the JUnit 4 dependency.
Exercise
A JUnit 4 test has @BeforeClass public static void init() and @Ignore @Test public void slowTest(). What annotations replace @BeforeClass and @Ignore in JUnit 5?
Show answer
@BeforeClass becomes @BeforeAll (the method still needs to be static by default), and @Ignore becomes @Disabled. You can also drop the public keyword.
Exercise
Rewrite assertEquals("total is wrong", 10, total); for JUnit 5.
Show answer
assertEquals(10, total, "total is wrong"); The failure message moves from the first argument to the last, and the import changes to org.junit.jupiter.api.Assertions.assertEquals.