Introduction to JUnit5

August 21, 2020 | No comments

JUnit 5 Introduction

JUnit 5 is the latest version of JUnit. It has a brand new architecture and more capabilities compared to the previous generation of JUnit. In this article, we will quickly go through some of the main features and concepts of JUnit 5 through hands on examples.

JUnit 5 Project

JUnit 5 is composed of mainly three sub-projects JUnit Platform, JUnit Jupiter, and JUnit Vintage. junit-jupiter-api (JUnit Platform): JUnit Jupiter API for writing tests and extensions.
junit-jupiter-engine(JUnit Jupiter) : JUnit Jupiter test engine implementation, only required atruntime.
junit-vintage-engine:(JUnit Vintage): To run tests wittern in older version of JUnit on JUnit 5 Platform.

This article is focused on JUnit5 with maven as a build tool. Other than the maven related configurations information in this articles holds true for other build environments also.
Java 8
JUnit 5 requires a minimum Java 8 version

JUnit 5 Artifacts

If you’re using Maven you can add the corresponding subprojects in your pom file (juinit-jupiter-api, junit-jupiter-engine, junit-vintage-engine). Starting with Juinit 5.4 we can also simply add aggregate artifact named junit-jupiter in our pom.xml to include all the JUnit 5 related dependencies to our project.
JUnit 5 Maven Dependencies

Main Features of JUnit 5

Following are the main features in JUnit 5
Supports Lambda expressions.
Nested Unit Tests
Parameterized Unit Tests
Conditional Test inclusion or exclusion
Extension Model

Main Annotations

Tests written in JUnit5 follow some standard approach and it is done by utilizing the capabilities provided by the JUnit 5 framework. The following are some of the annotations provided by JUnit 5. This is just to provide some quick pointers to the rich feature sets of JUnit 5.
Common annotations
@Test – denotes a test method, it does not take any arguments. 
@DisplayName – specifies a custom name for the test class or method. 
@DisplayNameGeneration - specify a generator to generate a custom name for method or class. 
@Nested - it marks a class a nested test 
@Disabled – prevents a test class or method from executing; 
@AfterEach – runs the annotated method before or after each test method in the same class. 
@AfterAll – runs the annotated method before any or after all of the test methods in the class.

Next: Writing first Test in JUnit 5


No comments :

Post a Comment

Please leave your message queries or suggetions.