JUnit 5 vs JUnit 4 Differences | Java Fresher
Fast recall
Quick revision
Core answer
@Before→@BeforeEach, @BeforeClass→@BeforeAll, @Ignore→@Disabled), exception testing moved to `assertThrows` (vs @Test(expected=)), the runner model @RunWith became the extension model `@ExtendWith`, it requires Java 8+, and it adds @DisplayName, @Nested, and first-class parameterized tests.Interview-ready guide
Interview answer
JUnit 5 is basically a ground-up rewrite of JUnit 4, and the differences fall into a few buckets. The biggest is architecture. JUnit 4 was a single jar with one runner mechanism, the RunWith annotation, and you could only have one runner per class. JUnit 5 is modular — it splits into the Platform that launches tests, the Jupiter API I write against, and Vintage that runs old JUnit 4 tests. And instead of a single runner, it uses extensions through the ExtendWith annotation, which you can stack, so I can combine, say, the Mockito extension and the Spring extension on the same class.
Then there are the renamed annotations, which is what trips up freshers. Before became BeforeEach, BeforeClass became BeforeAll and has to be static, and Ignore became Disabled, where I pass a reason. The imports also change — everything moves to org dot junit dot jupiter dot api.
Exception testing changed too. In JUnit 4 you wrote Test with expected equals the exception class, but that only checked the type and couldn't look at the message. In JUnit 5 I use assertThrows, which returns the exception so I can assert on its message. JUnit 5 also needs Java 8 or later because the API uses lambdas, and it adds nice things like DisplayName and Nested.
The practical mistake is mixing JUnit 4 and 5 annotations in the same class, which causes confusing behavior. To migrate, I add Vintage so old tests keep running, then convert class by class.
JUnit 5 vs JUnit 4 Differences | Java Fresher
Direct Answer
JUnit 5 is a modular rewrite of JUnit 4. Annotations were renamed (@Before→@BeforeEach, @BeforeClass→@BeforeAll, @Ignore→@Disabled), exception testing moved to assertThrows (vs @Test(expected=)), the runner model @RunWith became the extension model @ExtendWith, it requires Java 8+, and it adds @DisplayName, @Nested, and first-class parameterized tests.
Check your understanding
Answer these without looking back at the article.
- Can JUnit 5 and JUnit 4 tests coexist in the same project?
- What is @ExtendWith and how does it work?
- Why were the annotations renamed rather than keeping backward compatibility?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.