JUnit 5 vs JUnit 4

By | October 4, 2020

JUnit 5 aims to adapt the java 8 styles of coding and to be more robust and flexible than JUnit 4.

JUnit 5 vs JUnit 4 – Annotations

FEATURE JUnit 4 JUnit 5
Declare a test method @Test @Test
The annotated method executes before all test methods in the current class @BeforeClass @BeforeAll
The annotated method executes after all test methods in the current class @AfterClass @AfterAll
The annotated method executes before each test method @Before @BeforeEach
The annotated method executes after each test method @After @AfterEach
Disable a test method/class @Ignore @Disabled
Test factory for dynamic tests NA @TestFactory
Nested tests NA @Nested
Tagging and filtering @Category @Tag
Register custom extensions NA @ExtendWith

Architectural Difference

  • JUnit 4 library has everything bundled into single jar file.
  • Junit 5 is composed of 3 sub-projects i.e. JUnit Platform, JUnit Jupiter and JUnit Vintage.

JUnit Platform

     It basically defines the TestEngine API for developing new testing frameworks that run on the  JUnit Platform.

JUnit Jupiter

       It has basically all new JUnit annotations and TestEngine implementation to run tests written with these annotations.

JUnit Vintage

    It supports running JUnit 3 and JUnit 4 written tests on the JUnit 5 platform

Required JDK Version

  • Junit 4 requires Java 5 or higher.
  • Junit 5 requires Java 8 or higher.

Assertions

  • On Junit 4, org.junit.Assert class has all assert methods to validate expected and resulting outcomes. They accept an extra parameter for error message as the FIRST argument in the method signature. 
  • In JUnit 5, org.junit.jupiter.Assertions  class contains most of assert methods including additional assertThrows() and assertAll() methods. JUnit 5 assertions methods also have overloaded methods to support passing error message to be printed in case the test fails

Assumptions

In Junit 4, org.junit.Assumecontains methods for stating assumptions about the conditions in which a test is meaningful. It has the following five methods:

  • assumeFalse()
  • assumeNoException()
  • assumeNotNull()
  • assumeThat()
  • assumeTrue()

In Junit 5, org.junit.jupiter.api.Assumptionscontains methods for stating assumptions about the conditions in which a test is meaningful. It has the following three methods:

  • assumeFalse()
  • assumingThat​()
  • assumeTrue()

 Tagging and Filtering

  • In Junit 4, @category annotation is used.
  • In Junit 5, @tag annotation is used.

Test Suites

3rd Party Integration

  • In Junit 4, there is no integration support for 3rd party plugins. They have to rely on reflection.
  • JUnit 5 has basically dedicated sub-project for this purpose i.e. JUnit Platform defines the TestEngine API to develop a test framework that runs on the platform.

You May Also Like:

Junit 5 Architecture
JUnit 5 Annotations
JUnit 5 Maven Dependency
JUnit 5 with Gradle Dependency
JUnit 5 Test Lifecycle
JUnit 5 @BeforeAll annotation example
Unit 5 @AfterAll annotation example
JUnit 5 @BeforeEach and @AfterEach annotation Example
JUnit 5 Display Names
Assertions in JUnit 5 Examples
Third-party Assertion Libraries support in JUnit 5
JUnit 5 Assumptions Examples
Conditional Test Execution in JUnit 5
JUnit 5 Nested Tests Example
JUnit 5 @Tag Annotation example
Test Execution Order in Junit 5
Dependency Injection and Testing in JUnit 5
Test Interfaces and Default Methods in JUnit 5

That’s all about JUnit 5 vs JUnit 4
If you have any feedback or suggestion please feel free to drop in below comment box.

Leave a Reply

Your email address will not be published. Required fields are marked *