0
0
JunitHow-ToBeginner ยท 4 min read

How to Use JUnit with Maven: Setup and Example

To use JUnit with Maven, add the JUnit dependency in your pom.xml file under <dependencies>. Then write test classes in src/test/java and run tests using mvn test command.
๐Ÿ“

Syntax

To use JUnit with Maven, you need to add the JUnit dependency inside the <dependencies> section of your pom.xml. This tells Maven to download JUnit and include it in your test classpath.

Here is the basic syntax:

  • <dependency>: Defines a library your project needs.
  • <groupId>: The organization or group that provides the library.
  • <artifactId>: The specific library name.
  • <version>: The version of the library to use.
  • <scope>: Defines when the dependency is used; test means only during testing.
xml
<dependency>
  <groupId>org.junit.jupiter</groupId>
  <artifactId>junit-jupiter</artifactId>
  <version>5.9.3</version>
  <scope>test</scope>
</dependency>
๐Ÿ’ป

Example

This example shows a simple Maven project setup with JUnit 5. It includes the pom.xml dependency and a test class that checks if 2 + 2 equals 4.

Run mvn test in the project root to execute the test.

xml and java
<?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>junit-maven-demo</artifactId>
  <version>1.0-SNAPSHOT</version>
  <dependencies>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter</artifactId>
      <version>5.9.3</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.0.0</version>
        <configuration>
          <useModulePath>false</useModulePath>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

// src/test/java/com/example/CalculatorTest.java
package com.example;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class CalculatorTest {

    @Test
    void addition() {
        assertEquals(4, 2 + 2, "2 + 2 should equal 4");
    }
}
Output
[INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running com.example.CalculatorTest [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.012 s - in com.example.CalculatorTest [INFO] [INFO] Results: [INFO] [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 1.234 s [INFO] Finished at: 2024-06-01T12:00:00Z [INFO] ------------------------------------------------------------------------
โš ๏ธ

Common Pitfalls

Some common mistakes when using JUnit with Maven include:

  • Forgetting to set <scope>test</scope> for JUnit dependency, which bloats your production build.
  • Not using the correct JUnit version or mixing JUnit 4 and 5 dependencies.
  • Placing test classes outside src/test/java so Maven does not find them.
  • Not configuring the maven-surefire-plugin properly, causing tests not to run.

Example of a wrong dependency scope and the fix:

xml
<!-- Wrong: no scope, JUnit included in production -->
<dependency>
  <groupId>org.junit.jupiter</groupId>
  <artifactId>junit-jupiter</artifactId>
  <version>5.9.3</version>
</dependency>

<!-- Right: test scope only -->
<dependency>
  <groupId>org.junit.jupiter</groupId>
  <artifactId>junit-jupiter</artifactId>
  <version>5.9.3</version>
  <scope>test</scope>
</dependency>
๐Ÿ“Š

Quick Reference

Summary tips for using JUnit with Maven:

  • Always add JUnit dependency with test scope in pom.xml.
  • Write test classes under src/test/java following package structure.
  • Use mvn test to run tests from the command line.
  • Configure maven-surefire-plugin if you use JUnit 5 to ensure tests run correctly.
  • Keep JUnit versions consistent to avoid conflicts.
โœ…

Key Takeaways

Add JUnit dependency with test scope in your Maven pom.xml to use it only during testing.
Place your test classes in src/test/java so Maven can find and run them.
Run tests using the mvn test command to execute all JUnit tests.
Configure the maven-surefire-plugin properly for JUnit 5 to ensure tests run smoothly.
Avoid mixing JUnit 4 and 5 dependencies to prevent version conflicts.