JUnit 5 Maven
JUnit 5 is a popular testing framework for Java applications, and you can easily set up JUnit 5 in a Maven-based Java project. Here’s a step-by-step guide on how to configure JUnit 5 with Maven:
Create a Maven Project: If you don’t have an existing Maven project, you can create one using your preferred IDE or by using the following command in your terminal:
arduinomvn archetype:generate -DgroupId=com.example -DartifactId=myproject -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
Open pom.xml: Open the
pom.xml
file in your project directory. This is where you’ll configure your Maven dependencies.Add JUnit 5 Dependencies: Inside the
<dependencies>
section of yourpom.xml
, add the following dependencies for JUnit 5:xml<dependencies>
<!-- JUnit 5 API -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.0</version>
<scope>test</scope>
</dependency><!-- JUnit 5 Engine (allows running tests) -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.0</version>
<scope>test</scope>
</dependency>
</dependencies>
This configuration includes the JUnit Jupiter API and the JUnit Jupiter Engine, which is required for running JUnit 5 tests.
Configure the Maven Surefire Plugin: In the
pom.xml
, you also need to configure the Maven Surefire Plugin to run JUnit 5 tests. Add the following configuration under the<build>
section:xml<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>1.8.0</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
This configuration specifies the Surefire Provider for JUnit 5.
Write JUnit 5 Tests: Create your JUnit 5 test classes by annotating methods with
@Test
and using JUnit 5 assertions. Here’s a simple example:javaimport org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;public class MyTest {
@Test
void testAddition() {
int result = 1 + 1;
assertEquals(2, result);
}
}
Run Tests: You can run your JUnit 5 tests using Maven with the following command:
bashmvn test
Maven will discover and execute your JUnit 5 tests.
That’s it! You’ve successfully set up JUnit 5 in a Maven project. You can now write and run your tests to ensure the correctness of your Java code.
Demo Day 1 Video:
Conclusion:
Unogeeks is the No.1 Training Institute for Java Training. Anyone Disagree? Please drop in a comment
You can check out our other latest blogs on Java Training here – Java Blogs
You can check out our Best in Class Java Training details here – Java Training
Follow & Connect with us:
———————————-
For Training inquiries:
Call/Whatsapp: +91 73960 33555
Mail us at: info@unogeeks.com
Our Website ➜ https://unogeeks.com
Follow us:
Instagram: https://www.instagram.com/unogeeks
Facebook: https://www.facebook.com/UnogeeksSoftwareTrainingInstitute
Twitter: https://twitter.com/unogeeks