Skip to main content

Migrate to JUnit 5

JUnit 5 (also known as JUnit Jupiter) was released in 2017 and introduced significant improvements over JUnit 4. Migrating from JUnit 4 to JUnit 5 involves updating imports, annotations, and taking advantage of new features.

Package changes

JUnit 5 uses different package names and imports compared to JUnit 4.

CalculatorTest.java
import org.junit.Test;
import org.junit.Before;
import org.junit.After;
import org.junit.BeforeClass;
import org.junit.AfterClass;
import org.junit.Ignore;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertNotNull;

public class CalculatorTest {

@BeforeClass
public static void setUpClass() {
// Runs once before all tests
}

@Before
public void setUp() {
// Runs before each test
}

@Test
public void testAddition() {
assertEquals(4, 2 + 2);
}

@Ignore("Not implemented yet")
@Test
public void testSubtraction() {
assertTrue(true);
}

@After
public void tearDown() {
// Runs after each test
}

@AfterClass
public static void tearDownClass() {
// Runs once after all tests
}
}
info

JUnit 4 uses org.junit.* packages and requires public visibility for test classes and methods.

Exception testing

JUnit 5 introduces assertThrows() to replace the awkward @Test(expected=...) pattern from JUnit 4.

ExceptionTest.java
import org.junit.Test;

public class ExceptionTest {

@Test(expected = IllegalArgumentException.class)
public void shouldThrowException() {
throw new IllegalArgumentException("Invalid argument");
}
}
warning

The @Test(expected=...) pattern doesn't allow you to verify the exception message or other properties. Any code in the test method can throw the exception, making it unclear which statement is expected to fail.

Automated migration

Manual migration can be tedious and error-prone. OpenRewrite provides automated migration recipes.

The Moderne CLI allows you to run OpenRewrite recipes on your project without needing to modify your build files, against serialized Lossless Semantic Tree (LST) of your project for a considerable performance boost & across projects.

You will need to have configured the Moderne CLI on your machine before you can run the following command.

  1. If project serialized Lossless Semantic Tree is not yet available locally, then build the LST. This is only needed the first time, or after extensive changes:
shell
mod build ~/workspace/
  1. If the recipe is not available locally yet, then you can install it once using:
shell
mod config recipes jar install org.openrewrite.recipe:rewrite-testing-frameworks:LATEST
  1. Run the recipe.
shell
mod run ~/workspace/ --recipe org.openrewrite.java.testing.junit5.JUnit4to5Migration
tip

OpenRewrite's JUnit4to5Migration recipe automatically handles:

  • Updating imports and packages
  • Converting annotations (@Before@BeforeEach, etc.)
  • Migrating exception testing patterns
  • Updating assertions to JUnit 5 equivalents
  • Converting JUnit 4 rules to JUnit 5 extensions