VOOZH about

URL: https://www.geeksforgeeks.org/software-testing/junit-5-vs-junit-4/

⇱ JUnit 5 vs JUnit 4 - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

JUnit 5 vs JUnit 4

Last Updated : 26 Sep, 2025

JUnit is the most used testing framework in Java. Over time, it has evolved to introduce new features and improve flexibility. JUnit 4 was the standard for many years, but JUnit 5 has become the modern choice with a more modular architecture and enhanced functionality. This article explains the key differences between JUnit 4 and JUnit 5, their features and why migrating to JUnit 5 is recommended.

  • JUnit 4 (released in 2006) simplified Java testing by introducing annotations like @Test, @Before and @After.
  • JUnit 5 (released in 2017) is a major redesign, offering a modular approach, better integration with Java 8+ features and improved extensibility.

Example: Calculator Class

We’ll use a simple Calculator class with two methods: add() and subtract().

JUnit 5 Test Case Design for Calculator Java Class

JUnit 4 Test Case Design for Calculator Java Class

Difference between JUnit 5 and JUnit 4

TopicJUnit 5JUnit 4
ArchitectureModular and extensible, supports Java 8 features including lambdas.Monolithic architecture, limited Java 8 support.
AnnotationsIntroduces new annotations: @BeforeEach, @AfterEach, @BeforeAll, @AfterAll.Provides older set: @Before, @After, @BeforeClass, @AfterClass.
Test ExtensionsSupports powerful extension model with @ExtendWith for parameter resolution, post-processing, etc.Limited extension support; relies on test runners.
Parameterized TestsBuilt-in support using @ParameterizedTest, @ValueSource, etc.Requires @RunWith(Parameterized.class).
Conditional Test ExecutionProvides annotations like @EnabledOnOs, @EnabledIf.Very limited conditional support.
Dynamic TestsSupports runtime test generation via @TestFactory.Only static test methods supported.
AssertionsMore flexible with assertAll, multiple assertions per test.Basic assertions from org.junit.Assert.
Tagging & FilteringSupports tagging with @Tag for grouping/filtering tests.Limited tagging support.
IDE SupportGrowing support in modern IDEs.Mature support across IDEs.
CompatibilityNot backward-compatible with JUnit 4. Migration is required.Has backward compatibility with JUnit 3.
Comment

Explore