Discover how switching from JUnit 4 to JUnit 5 can make your tests simpler and more powerful!
JUnit 4 vs JUnit 5 (Jupiter) differences - When to Use Which
Imagine you have many test cases written in JUnit 4 for your Java project. Now, you want to add new features and improve your tests, but the old way feels limiting and messy.
Using JUnit 4, tests can become hard to organize and extend. It lacks modern features like better lifecycle control and flexible assertions, making tests slower to write and harder to maintain.
JUnit 5 (Jupiter) introduces a fresh, modular design with powerful new features. It makes writing and managing tests easier, faster, and more flexible, so you can focus on quality without frustration.
@Test
public void testOld() {
assertEquals(5, calculator.add(2, 3));
}@Test
void testNew() {
assertEquals(5, calculator.add(2, 3));
}JUnit 5 enables modern, clean, and maintainable tests with better control and new powerful features.
A developer upgrading a large project from JUnit 4 to JUnit 5 can write more expressive tests and use new annotations to better organize test setup and teardown.
JUnit 4 is older and less flexible.
JUnit 5 offers modularity and new features.
Upgrading improves test clarity and maintenance.
Practice
Solution
Step 1: Understand JUnit 4 test method visibility
JUnit 4 requires test methods to be declared as public for the framework to recognize and run them.Step 2: Understand JUnit 5 test method visibility
JUnit 5 allows test methods to have any visibility, including package-private, making it more flexible.Final Answer:
JUnit 4 requires test methods to be public, but JUnit 5 does not. -> Option AQuick Check:
JUnit 4 public test methods = true [OK]
- Thinking JUnit 5 also requires public test methods
- Assuming both versions have the same visibility rules
- Confusing private and public requirements
@Before from JUnit 4 in JUnit 5?Solution
Step 1: Identify JUnit 4 setup annotation
JUnit 4 uses@Beforeto run code before each test method.Step 2: Identify JUnit 5 equivalent annotation
JUnit 5 replaces@Beforewith@BeforeEachto clarify it runs before each test method.Final Answer:
@BeforeEach -> Option DQuick Check:
@Before in JUnit 4 = @BeforeEach in JUnit 5 [OK]
- Choosing @BeforeAll which runs once before all tests
- Using non-existent annotations like @Setup or @Init
- Confusing @BeforeEach with @AfterEach
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
class CalculatorTest {
@Test
void addTest() {
int result = 2 + 3;
assertEquals(5, result);
}
}What will be the test result when running this test?
Solution
Step 1: Check method visibility in JUnit 5
The test method is package-private (no modifier), which is allowed in JUnit 5.Step 2: Verify assertion correctness
The assertion compares 5 and result (2+3=5), so it matches and passes.Final Answer:
Test passes successfully -> Option AQuick Check:
2 + 3 = 5, assertion true [OK]
- Assuming test methods must be public in JUnit 5
- Thinking missing @BeforeEach causes failure here
- Confusing assertion order causing failure
import org.junit.Before;
import org.junit.Test;
public class SampleTest {
@Before
public void setup() {
System.out.println("Setup");
}
@Test
void testMethod() {
System.out.println("Test");
}
}What is the main issue when running this test?
Solution
Step 1: Check test method visibility requirement in JUnit 4
JUnit 4 requires test methods to be public, but here testMethod() has default (package-private) visibility.Step 2: Verify @Before method correctness
The setup() method is public and correctly annotated, so no issue there.Final Answer:
The test method must be public in JUnit 4 -> Option BQuick Check:
JUnit 4 test methods must be public [OK]
- Thinking @Before is invalid in JUnit 4
- Assuming setup must be static
- Believing test runs despite non-public test method
Solution
Step 1: Identify visibility requirements in JUnit 5
JUnit 5 does not require test methods to be public; package-private is allowed.Step 2: Check other migration steps
Replacing@Beforewith@BeforeEachand updating imports to Jupiter packages are required for migration.Final Answer:
Make all test methods public -> Option CQuick Check:
JUnit 5 relaxes method visibility rules [OK]
- Thinking test methods must be public in JUnit 5
- Forgetting to update import statements
- Missing @Before to @BeforeEach replacement
Ready to prove it?
Pass the test and this pattern moves unknown
