Search⌘ K
AI Features

Difference between JUnit 4 and JUnit 5

Explore the key distinctions between JUnit 4 and JUnit 5, focusing on annotation changes, test lifecycle hooks, and Java Development Kit compatibility. This lesson prepares you to migrate your unit tests to JUnit 5 with clarity on updated features and support.

This lesson will help us understand the differences between JUnit 4 and JUnit 5.

JUnit 4 and JUnit 5 comparison

The table below shows a side-by-side comparison of annotations used in JUnit 4 and JUnit 5.

JUnit 5

JUnit 4

@Test

@Test

@BeforeEach

@Before

@BeforeAll

@BeforeClass

@AfterEach

@After

@AfterAll

@AfterClass

@Disabled

@Ignore

@Tag

@Category

@ExtendWith

@RunWith

Code example using JUnit 4

Below is a unit test code written in JUnit 4.

JUnit5 (Java 1.8)
package io.educative.junit5;
import org.junit.*;
public class HelloWorldTest {
private Calculator calculator = new Calculator();
@BeforeClass
public static void setupClass() {
System.out.println("Inside @BeforeClass method");
}
@AfterClass
public static void tearDownClass() {
System.out.println("Inside @AfterClass method");
}
@Before
public void setup() {
System.out.println("Inside @Before method");
}
@After
public void tearDown() {
System.out.println("Inside @After method");
}
@Test
public void testAddition() {
int result = calculator.add(1, 2);
Assert.assertEquals(3, result);
}
@Test
@Ignore
public void testDivison() {
int result = calculator.divide(2, 1);
Assert.assertEquals(2, result);
}
}
  • We define the test with the @Test annotation from the package org.junit.
  • We use the @BeforeClass, @Before, @After, and @AfterClass are used to hook into the life cycle of the JUnit 4 tests.
  • We use the @Ignore annotation to skip the execution of a given test.

Code example using JUnit 5

Below is a unit test code written in JUnit 5.

JUnit5 (Java 1.8)
package io.educative.junit5;
import org.junit.jupiter.api.*;
public class HelloWorldTest {
private Calculator calculator = new Calculator();
@BeforeAll
static void setupClass() {
System.out.println("Inside @BeforeAll method");
}
@AfterAll
static void tearDownClass() {
System.out.println("Inside @AfterAll method");
}
@BeforeEach
void setup() {
System.out.println("Inside @BeforeEach method");
}
@AfterEach
void tearDown() {
System.out.println("Inside @AfterEach method");
}
@Test
public void testAddition() {
int result = calculator.add(1, 2);
Assertions.assertEquals(3, result);
}
@Test
@Disabled
public void testDivison() {
int result = calculator.divide(2, 1);
Assertions.assertEquals(2, result);
}
}
  • We define the test with the @Test annotation from the org.junit.jupiter.api package.
  • We use @BeforeAll, @BeforeEach, @AfterEach, and @AfterAll to hook into the life cycle of the JUnit5 tests.
  • The @Disabled annotation is used to skip the execution of a given test.

Java Development Kit (JDK) compatibility

The main difference to keep in mind before migrating to JUnit 5 is the JDK Compatibility.

JUnit 4 supports JDK 5 and later versions. JUnit 5 supports JDK 8 and later versions.