In this tutorial we will learn how to create a JUnit 5 project using IntelliJ. We will be at first creating a simple Maven Project, then we will add a basic Class and a JUnit Test for it.
Creating an IntelliJ Project:
Requirements: Download IntelliJ Community edition from: https://www.jetbrains.com/idea/download/
Install IntelliJ Idea, then launch it:
- Create a new Project using your favorite framework (e.g. Maven):
- Fill up the Project coordinates (Group, Artifact, Version)
- Next add a Java Class
Here is our basic class:
public class UnitTestDemo { public int sum(int x, int y) { return x+y; } }
Next, right-click on your Class and choose “Generate” to generate a Test Class:
Select the Class name to be generated, the methods to be tested and the testing framework:
As you can see from the above image, the JUnit library has not been found in your project. Clicking on Fix you can choose where to install the JUnit library.
Here is the generated class:
import static org.junit.jupiter.api.Assertions.*; class UnitTestDemoTest { @org.junit.jupiter.api.Test void sum() { } }
Add an example test to verify the sum method:
import static org.junit.jupiter.api.Assertions.*; class UnitTestDemoTest { @org.junit.jupiter.api.Test void sum() { UnitTestDemo test = new UnitTestDemo(); assert(test.sum(3,3) == 6); } }
Here is how your JUnit project should look like:
src ├── main │ ├── java │ │ └── UnitTestDemo.java │ └── resources └── test └── java └── UnitTestDemoTest.java
And this is the pom.xml used by our project:
<?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>com.example</groupId> <artifactId>junitdemo</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>RELEASE</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>RELEASE</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>RELEASE</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>RELEASE</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>RELEASE</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>RELEASE</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>RELEASE</version> <scope>test</scope> </dependency> </dependencies> </project>
Now right-click on the Test class and check that the Test passed:
Congratulations! You just managed to create your first JUnit 5Project with IntelliJ Idea!