Junit is a testing framework for the Java programming languages. The Junit 5 version has been redesigned to solve certain challenges of previous versions. It also offers a new architecture, which offers the possibility of creating a hierarchy of tests, with assertions and assumptions, with dynamic and parameterized tests. This article is a short introduction to Junit 5 to help you understand the new architecture and functionalities that come with it.
As a unit-testing framework for the Java programming language, Junit is a very important tool for a test-driven development approach. It is part of a family of testing frameworks, called xUnit. Junit is tied at compiling time as a JAR and is the most frequently included external library in Java projects.
TDD (Test Driven Development) is a software development process that is based on repeating a short cycle: first, the requirements are transformed in specific test cases, and then the software is improved so that it can pass the new tests. This is different from the approach where the software was developed without proving it can meet all requirements.
Some of the benefits of TDD are:
The challenges of Junit 4
- The fact that the programmer has clear targets
The code is safer
The correct code can be isolated
New functionalities can be more easily introduced
The tests document the application
Junit 4, which appeared in 2006, has a simple and monolithic architecture. The entire functionality is concentrated in a single JAR file. Despite its apparent simplicity, there were a couple of problems early on, which became worse as time went by.
The fact that the existent API is not flexible forced the IDEs and tools which used Junit to be completely linked with it. It was necessary to access the internal implementation of JUNit or even use reflection to obtain the nece4sary information.
Therefore, because the same JAR file was used by everyone and IDEs were strongly linked to it, the ways in which it could evolve were seriously hindered. The changing of a variable or a private method could impact those who used it externally. A new API, designed for these types of tools and a new architecture were necessary to ensure its further evolution.
A new modular approach
A new approach, a modular one, was needed to allow Junit to evolve. It required:
- An API for writing tests, dedicated to developers first of all
A mechanism for discovering and running tests
An API which allows an easy interaction with IDEs and test running tools
As a result the Junit 5 architecture has 3 modules:
- Junit Platform
Junit Platform is used for launching the testing framework on a Java virtual machine. It also offers an API for launching test from the console, IDEs or other tools.
JUnit Jupiter is a combination between the new programming model and the extension model for writing tests and extensions in Junit 5.
Junit Vintage offers a testing engine to run Junit 3 and Junit 4 test on the new platform, ensuring the necessary compatibility.
Interested in JUnit? Check out our trainings.
Java and Web Technologies Expert