Search code examples
javamavenjunit

Maven package org.junit.jupiter.api does not exist


I'm working on a Java project that requires testing using JUnit. I've added the necessary JUnit dependency to my pom.xml file as shown below:

<dependencies>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.7.0</version>
    </dependency>
</dependencies>

After adding the dependency, I refreshed the Maven project to ensure it's recognized.

In my test file, I've imported JUnit using the following imports:

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

The code editor doesn't show any import errors, indicating that JUnit is being recognized. However, when I attempt to run any of the tests, I encounter the following error message:

java: package org.junit.jupiter.api does not exist

It seems like there might be an issue with the project setup or configuration that's causing this error. I'd appreciate any guidance on how to resolve this problem.


Solution

  • You will need to add a JUnit implementation to your project, not just the API!

    Refer to the User Guide for setting up your maven build.

    What your dependency also is lacking is the test scope: you should add <scope>test</scope> to your maven dependency. Otherwise you will package your maven dependency with your application, where it is not needed (tests are usually not used at runtime).

    Overall your dependency should look something like that shown on a Maven Repository site such as this.

    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter</artifactId>
        <version>5.10.0</version> <!-- can be omitted when using the BOM -->
        <scope>test</scope>
    </dependency>
    

    If you use the Maven BOM for JUnit, you could skip the version there - details are described in the user guide I linked above.