JUnit 4 and JUnit 5 both are Java-based testing frameworks, The JUnit 5 is the advanced testing framework when compared with JUnit 4. The JUnit provides a lot of features like Annotation-based coding, parallel test execution, and other features.
Difference between JUnit 5 and JUnit 4
Topic |
JUnit 5 |
JUnit 4 |
---|---|---|
Architecture |
JUnit 5 supports designs with more modular and extensible architecture, supports Java 8 features and it can support lambda expressions also. |
JUnit 4 follows the monolithic architecture and limited support for lambda expression. |
Annotations |
The JUnit 5 introduces new annotations like @Test, @BeforeEach, @AfterEach, @BeforeAll, and @AfterAll. And it supports additional annotations also for controlling the life cycle of the test cycle. |
JUnit 4 supports only limited Annotations while comparing with JUnit 5, In JUnit 4 these annotations are used @Test, @Before, @After, @BeforeClass, @AfterClass. |
Test Extensions |
The JUnit 5 Introduces a powerful extension model that is @ExtendWith. It can parameter resolution, test instance post-processing, and others |
JUnit 4 lacks built-in support for extensions. The test runners are the primary extensions in the JUnit 4 framework. |
Parameterized Tests |
JUnit 5 supports Parameterized Tests without the need for the custom runner. for Parameterized Tests we can use @ParameterizedTest and @ValueSource Annotations in JUnit 5 |
In JUnit 4 we use @RunWith(Parameterized.class) for Parameterized Test. |
Conditional Test Execution |
JUnit 5 introduces @EnabledOnOs and @EnabledIf and more for conditional test execution. |
The JUnit 4 has limited support for conditional test execution. While comparing with JUnit 5. |
Dynamic Tests |
The JUnit 5 introduces a new annotation for Dynamic tests which is @TestFactory. It can allow us to run tests in runtime. |
The JUnit 4 primarily relies on static tests means we are unable to test the application in runtime. |
Assertions |
The JUnit 5 introduces more flexible Assertions class with different methods like assertAll and other supporting multiple Assertions within a single test. |
JUnit 4 provides basic Assertions by the org.junit.Assert class while comparing with JUnit 5. |
Tagging and Filtering |
The JUnit support Tagging and Filtering tests using annotation like @Tag. |
JUnit 4 have limited support for Tagging and Filtering tests. |
IDE Support |
JUnit 5 have growing support in IDEs, and with continued improvement. |
JUnit 4 have mature support in various IDEs. |
Compatibility |
The JUnit 5 don’t have any backward compatibility with JUnit 4, but migration is required. |
It has backward compatibility with JUnit 3. |
Now, we will get to know the JUnit 5 vs JUnit 4 pragmatically for better understanding the concept. For let’s take an example: One Calculator class for adding two numbers and subtraction of two numbers. And the outputs show only test is pass or not only.
Example Calculator Class
public class Calculator {
public int add( int a, int b) {
return a + b;
}
public int subtract( int a, int b) {
return a - b;
}
} |
JUnit 5 Test Case Design for Calculator Java Class
//JUnit 5 Test case design for Calculator java class import org.junit.jupiter.api.*;
public class CalculatorJUnit5Test {
private Calculator calculator;
@BeforeEach
void setUp() {
calculator = new Calculator();
System.out.println( "Setting up for test" );
}
@AfterEach
void tearDown() {
calculator = null ;
System.out.println("after test);
}
@Test
void testAddition() {
System.out.println( "addition test" );
assertEquals( 4 , calculator.add( 2 , 2 ));
}
@Test
void testSubtraction() {
System.out.println( "subtraction test" );
assertEquals( 2 , calculator.subtract( 4 , 2 ));
}
} |
JUnit 4 Test Case Design for Calculator Java Class
//JUnit 4 Test case design for Calculator java class import org.junit.*;
public class CalculatorJUnit4Test {
private Calculator calculator;
@Before
public void setUp() {
calculator = new Calculator();
System.out.println( "Setting up for test" );
}
@After
public void tearDown() {
calculator = null ;
System.out.println( "after test" );
}
@Test
public void testAddition() {
System.out.println( "addition test" );
assertEquals( 4 , calculator.add( 2 , 2 ));
}
@Test
public void testSubtraction() {
System.out.println( "subtraction test" );
assertEquals( 2 , calculator.subtract( 4 , 2 ));
}
} |
Conclusion
The JUnit 5 testing framework is considered as more and modern testing framework while comparing with JUnit. The JUnit 5 provides better support for dynamic test cases as well as contemporary Java features. And one more thing is JUnit 5 Supports Java 8 features like lambda expressions and others. The JUnit 5 introduces lot of new Annotations for controlling the test life cycle and it improves the test organization as well when comparing with JUnit 4.