junit maven dependency
To use JUnit with Maven, you need to add the JUnit dependency to your project's pom.xml
file. Here are the steps to do so:
Open your project's
pom.xml
file in a text editor or IDE.Add the following
<dependency>
element inside the<dependencies>
element:
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency>
This will add JUnit 4.13.2 as a test-scoped dependency for your project.
Save the
pom.xml
file.Refresh your project in your IDE or run
mvn clean install
from the command line to update your project's dependencies.
Once you have added the JUnit dependency to your project, you can write and run JUnit tests as usual. By default, Maven runs any tests in the src/test/java
directory when you run mvn test
.
If you are using JUnit 5, you need to add the following dependency to your pom.xml
file instead:
<dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.8.2</version> <scope>test</scope> </dependency>
And, you also need to add the following Surefire plugin configuration in pom.xml
to run JUnit 5 tests:
<build> <plugins> <plugin> <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.2</version> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-engine</artifactId> <version>5.8.2</version> </dependency> </dependencies> </plugin> </plugins> </build>
This will configure the Surefire plugin to run JUnit 5 tests.