1. What is JUnit 5?
JUnit is one of the most popular unit-testing frameworks in the Java ecosystem.
Unlike previous versions of JUnit, JUnit 5 is composed of several different modules from three different sub-projects.
JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage
JUnit Platform - foundation layer which enables different testing frameworks to be launched on the JVM
Junit Jupiter - is the JUnit 5 test framework which is launched by the JUnit Platform
JUnit Vintage - legacy TestEngine which runs older tests
1.1. Requirements:
JUnit 5 needs already installed to run:
- at least Java 8
- Maven / Gradle (In this tutorial we'll be using Maven)
The JUnit 5 version contains a number of exciting innovations, with the goal to support new features in Java 8 and above, as well as enabling many different styles of testing.
If you want to know more about JUnit, refer to the official documentation.
2. Example project structure
This is the basic template:
. ├── pom.xml └── src ├── main │ ├── java │ │ └── SourceCodeClass.java │ └── resources └── test └── java └── TestClass.java
2.1. pom.xml
There is pom.xml file, which is config file for our Java project using Maven.
According to https://maven.apache.org/guides/introduction/introduction-to-the-pom.html
A Project Object Model or POM is the fundamental unit of work in Maven. It is an XML file that contains information about the project and configuration details used by Maven to build the project. It contains default values for most projects. Examples for this is the build directory, which is
target
; the source directory, which issrc/main/java
; the test source directory, which issrc/test/java
; and so on. When executing a task or goal, Maven looks for the POM in the current directory. It reads the POM, gets the needed configuration information, then executes the goal.Some of the configuration that can be specified in the POM are the project dependencies, the plugins or goals that can be executed, the build profiles, and so on. Other information such as the project version, description, developers, mailing lists and such can also be specified.
The simplest example of pom.xml
that enables running tests in IDE and shell looks like this:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>JUnit5_Overview<</artifactId> <version>1.0-SNAPSHOT</version> // This is depedency that we need to add to usew JUnit5 in our project <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <version>5.7.2</version> <scope>test</scope> </dependency> </dependencies> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> </properties> // This plugins enables running tests from shell <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> </plugin> <plugin> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0-M1</version> </plugin> </plugins> </build> </project>
Maven Surefire Plugin is used during the test phase of the build lifecycle to execute the unit tests of an application. It can be used with JUnit, TestNG or other testing frameworks.
By default, the Surefire Plugin will automatically include all test classes with the following wildcard patterns, and execute them as unit tests:
Pattern |
---|
**/Test*.java |
**/*Test.java |
**/*Tests.java |
**/*TestCase.java |
2.2. main / test folders
Inside Overview/src
folder you can see two subfolders:
main
- here are our source codes, with working functionalitiespublic class SourceCodeClass { public static void main(String[] args) { System.out.printf("This is our main class!"); } }
test
- here are test classes, that check whether source code is working properly. This is where our JUnit5 is used →TestClass.java
A test method is a method that fulfills these three requirements:
- A test method isn't
private
orstatic
- Its return type must be
void
. - A test method must be annotated with the
@Test
annotation.
// We import jupiter to our class to use proper annotation import org.junit.jupiter.api.*; class TestClass { @Test void exampleTest() { System.out.println("example test method"); } }
- A test method isn't