Updated April 3, 2023
Definition of JUnit 5 Maven Dependency
JUnit 5 is a testing framework developed from scratch with nothing copied JUnit 4. Maven facilitates easy and quicker project builds for developers and helps them in managing project lifecycle efficiently. When JUnit 5 Projects are implemented in Maven platforms, it brings its own dependencies that have to be managed to increase the productivity of developers and avoid wastage of time and effort.
Maven identifies the usage of unwanted libraries by virtue of its dependencies with main libraries that are included as part of the JUnit 5 Project. Maven goes through the project’s details files and understands the dependency structure and removes the unwanted stuff included by the design. Maven brings in the required control to manage any such scenarios and keep the growth of unwanted libraries under check.
Features of JUnit 5 Dependency
Due to dependencies of one library has with another one, there is a tendency of duplicate dependencies growing within the JUnit 5 project. Maven with the features explained below controls such growth.
|1||Mediating Dependencies||Maven resolves the conflict arising out of multiple versions and chooses the right version for use. If there are two dependencies at the same level to be used, the first one will be chosen.|
|2||Managing Dependencies||In case transitive dependencies, Maven chooses directly the version of the dependency to be consumed.|
|3||Scoping the Dependencies||Depending on the current status of the project Maven chooses the right dependencies and include them in the project build|
|4||Excluding the dependencies||Maven excludes any transitive dependencies that are not needed|
|5||Optional usage of Dependencies||Maven marks any dependencies that are not required as optional.|
JUnit 5 Maven – Two Dependencies
Two dependencies that help developers to configure JUnit 5 tests in the Maven platform and perform the tests are
This dependency is basically a public application interface (API) that facilitates the development of tests and extension functions.
It consists of the following packages
|Jupiter.api||Used for developing test cases|
|Jupiter.api.condition||To enable or disable tests in JUnit Jupiter which are annotation based|
|Jupiter.api.extension||Used for developing extensions|
|Jupiter.api.function||To use functional interfaces in JUnit Jupiter|
|Jupiter.api.io||Input output related support in JUnit Jupiter|
|Jupiter.api.parallel||This API influences execution of tests in parallel mode|
It uses the following interfaces
|Class descriptor||Encapsulates the functionality of a given class|
|Class Orderer||Ordering top level test classes|
|Class Orderer context||Encapsulates the context in which the above interface is invoked|
|Display name generator||Service provider interface to display program name|
|Method Descriptor||Functionality of the given method is encapsulated|
Several classes are used in this dependency
|Assertions||Methods that support Asserting conditions are grouped in this class|
|Assumptions||Methods that support Assumption based Conditional test execution are grouped in this class|
|Dynamic Container||Refers to containers that are generated during run time dynamically|
|Dynamic Test||Refer to tests that are created dynamically during run time|
|Class orderer||Classes are sorted on its name, display name, order annotation and randomly|
Some of the annotation interfaces used are
|Annotation interfaces Name||Description|
|Afterall||After completing all the tests in the current class the annotated method would be executed|
|Aftereach||After completing each and every test in the current class the annotated method would be executed|
|Beforeall||Before completing all the tests in the current class the annotated method would be executed|
|Beforeeach||Before completing each and every tests in the current class the annotated method would be executed|
Junit-jupitar-Engine dependency has the test engine on which the unit tests are executed during runtime. This dependency is dependent on junit-jupitar-api, hence adding this dependency adds both of these dependencies in the class definition. Discovery and running of tests are facilitated by TestEngine for a programming model. JUnit has a TestEngine that discovers tests using Jupiter programming model and executes them.
Unique ID and discover tests are provided by TestEngine from Engine Discovery Requests and these tests are executed according to requests. In the development environment, @Testable annotation is recommended for test recovery. The annotations @Test and @TestFactory are meta annotated with @Testable.
The following methods are used in this dependency
|Getid||Return the unique ID of the Test Engine|
|Discover||It discovers the test as per the request|
|Execute||Executes the tests as per the request submitted to it|
|Getgroupid||It gets JAR’s group id.|
|Getartifactid||It gets JAR’s artifact id|
|Getversion||Test Engine’s version is returned|
Project Technologies Used
Projects in Maven are managed through an XML file that holds the information about the project and its configuration details, used during build stage of the project. In most cases, this file contains default values. XML file otherwise known as Project Object Model (POM) is the basic unit of Maven.
Maven looks for this XML file in the current directors and uses the information in it to execute and achieve the goal. Some of the configuration details available in XML file are a. Project dependencies, b. plugins, c. goals, d, profiles of the build, e. version, f. developers details, g. mailing list.
Maven’s default POM is known as Super POM. It is usually extended to all POMs unless developers want it different.
A POM should contain root, version, groupid, artifactid, version
JUnit 5 Maven Dependency Example
JUnit 5 Jupiter Engine.
<dependency> <groupId> ….. </groupid> <artifactId> ….. </artifactId> <version> ….. </version> <scope> ….. </scope> </dependency>
Maven sunfiire plugins to run tests
<?xml version = “…” encoding =” .. “?> <project xmlns =”http://.......” xmlns:xsi=”http://......” xsi:schemaLocation=”http://.............>” <modelVersion> …. </modelVersion> <groupId> ….. </groupid> <artifactId> ….. </artifactId> <version> ….. </version> <properties> <java,version> …. </java.version> <junit.jupiter.version> … </junit-jupiter.version> <project.build.sourceEncoding> … </project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId> ….. </groupid> <artifactId> ….. </artifactId> <version> ….. </version> <scope> ….. </scope> </dependency> …… </dependencies> <build> <plugins> <plugin> <groupId> ….. </groupid> <artifactId> ….. </artifactId> <version> ….. </version> <configuration> <source> … </source> <target> … </target> </configuration> </plugin> </plugins> </build> </project>
In this article, We had understood how maven dependencies help developers to execute tests smoothly and speed up the testing process.
This is a guide to JUnit 5 Maven Dependency. Here we discuss the Definition, features, methods and examples with code implementation. You may also have a look at the following articles to learn more –