This article will tell you how to add the JUnit 5 java library into the eclipse project. It introduces two methods, one is to add the JUnit 5 java library as standalone jar files to the eclipse project, the other is how to add JUnit 5 dependency in pom.xml of the eclipse maven project.
1. How To Add JUnit 5 Java Library Into Eclipse Maven Project.
1.1 Add standalone JUnit library in the eclipse project.
- Create a java maven project using eclipse.
- Right-click the eclipse java project.
- Click the Properties menu item in the popup menu list.
- Click Java Build Path in the left panel.
- Click the Libraries tab in the right panel.
- Click the Add Library… button in the right panel.
- Choose JUnit in the popup Add Library dialog, then click the Next button.
- Choose JUnit5 from the JUnit library version drop-down list, then click Finish.
- Click the Apply and Close button in the eclipse Properties window to apply the changes.
- Now you can find the JUnit library jar files in the project-name/JUnit 5 subfolder in the eclipse left side Project Explorer panel.
1.2 How to add JUnit 5 dependency in pom.xml.
- To run JUnit 5 in an eclipse maven project, you need to add the below dependencies in the project pom.xml file.
<dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.4.0</version> </dependency> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-runner</artifactId> <version>${junit.platform.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-launcher</artifactId> <version>${junit.platform.version}</version> <scope>test</scope> </dependency>
- But the above maven dependencies configurations may throw an error like Missing artifact org.junit.platform:junit-platform-runner:jar: 5.4.0. You can see the error at the beginning of the <dependency> XML element, and when you mouse over the error, it will show the error message.
- The error means there is no artifact org.junit.platform:junit-platform-runner:jar: 5.4.0 in the central maven repository. So you should add the existing JUnit dependency jar file version for the dependency.
- Now you can follow section 2.1 to add JUnit standalone library jar files into you eclipse project, then find the related jar version under the project-name/JUnit 5 subfolder.
- From the above picture, we can see that the org.junit.platform group artifacts version should be 1.4.0. So we change the dependencies XML data to the below.
<dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.4.0</version> </dependency> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-runner</artifactId> <version>1.4.0</version> <scope>test</scope> </dependency> <dependency> <groupId>org.junit.platform</groupId> <artifactId>junit-platform-launcher</artifactId> <version>1.4.0</version> <scope>test</scope> </dependency>
- Now when you save the maven project, the Missing artifact org.junit.platform:junit-platform-runner:jar: 5.4.0 error should disappear.