Maven Compilation error [package org.testng.annotations does not exist]
Categories:
Resolving 'package org.testng.annotations does not exist' in Maven

This article guides you through diagnosing and fixing the common Maven compilation error related to missing TestNG annotations, ensuring your tests compile and run correctly.
Encountering a package org.testng.annotations does not exist error during a Maven build is a common frustration for developers working with TestNG. This error typically indicates that the Maven compiler cannot locate the TestNG library, which provides essential annotations like @Test, @BeforeMethod, and @AfterClass. This guide will walk you through the primary causes of this issue and provide step-by-step solutions to get your Maven project compiling successfully.
Understanding the Root Cause
The package org.testng.annotations does not exist error fundamentally means that the Java compiler, invoked by Maven, cannot find the TestNG classes it needs to resolve references in your source code. This usually boils down to one of two main problems:
- Missing or Incorrect Dependency: The TestNG dependency is either not declared in your
pom.xmlfile, or it's declared incorrectly (e.g., wrong groupId, artifactId, or version). - Incorrect Scope: The TestNG dependency is declared with a scope that prevents it from being available during the compilation phase (e.g.,
runtimeinstead ofcompileortest). - Maven Repository Issues: Maven might be unable to download the TestNG artifact from the configured repositories, or your local repository might be corrupted.
flowchart TD
A[Maven Build Start] --> B{Compile Phase}
B --> C{Java Compiler Invoked}
C --> D{Source Code Analysis}
D -- Uses TestNG Annotations --> E{TestNG Library Lookup}
E -- Library Not Found --> F["Error: package org.testng.annotations does not exist"]
E -- Library Found --> G[Compilation Successful]
F --> H[Check pom.xml Dependency]
H --> I[Check Dependency Scope]
I --> J[Update Maven Local Repo]
J --> AFlowchart illustrating the Maven compilation process and potential failure points for TestNG dependency.
Solution 1: Verify and Correct TestNG Dependency in pom.xml
The most frequent cause is an improperly configured TestNG dependency. Ensure that your pom.xml file includes the correct groupId, artifactId, and version for TestNG. It's crucial that the scope is set to test or compile to make it available during the compilation of your test classes.
<project ...>
<dependencies>
<!-- Other dependencies -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.7.1</version> <!-- Use the latest stable version -->
<scope>test</scope>
</dependency>
</dependencies>
</project>
Correct TestNG dependency declaration in pom.xml with test scope.
Solution 2: Address Dependency Scope Issues
Maven dependency scopes dictate when a dependency is available on the classpath. If TestNG is declared with a scope like runtime or provided (and not correctly handled), it won't be available during the compile phase for your test sources. For test frameworks like TestNG, the test scope is almost always the correct choice, as it makes the dependency available for compiling and running tests, but not for the main application code.
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.7.1</version>
<scope>compile</scope> <!-- If TestNG annotations are used in main source, though rare -->
</dependency>
<!-- OR, more commonly -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.7.1</version>
<scope>test</scope> <!-- Recommended for test frameworks -->
</dependency>
Examples of compile and test scopes for TestNG.
Solution 3: Clean and Update Maven Local Repository
Sometimes, your local Maven repository (~/.m2/repository on Linux/macOS, C:\Users\YourUser\.m2\repository on Windows) can become corrupted, or a previous download of TestNG might have been incomplete. Cleaning your local repository and forcing Maven to re-download dependencies can resolve such issues.
1. Clean Maven Project
Open your terminal or command prompt, navigate to your project's root directory (where pom.xml is located), and run the Maven clean command. This removes the target directory.
2. Delete TestNG from Local Repository
Manually navigate to your local Maven repository (e.g., ~/.m2/repository/org/testng) and delete the testng folder. This ensures Maven will re-download it.
3. Rebuild Maven Project
Execute a full Maven build, which will force Maven to re-download all necessary dependencies, including TestNG.
Maven Clean
mvn clean
Maven Rebuild
mvn clean install
~/.m2/repository folder is a more drastic measure that can fix many dependency issues, but it will require Maven to re-download all project dependencies, which can take a significant amount of time depending on your project's complexity and internet speed.By systematically checking your pom.xml for correct TestNG dependency declaration and scope, and ensuring your local Maven repository is in a healthy state, you should be able to resolve the package org.testng.annotations does not exist compilation error and proceed with your TestNG-based testing.