Maven 3 and JUnit 4 compilation problem: package org.junit does not exist

Are Husby picture Are Husby · May 1, 2011 · Viewed 198.8k times · Source

I am trying to build a simple Java project with Maven. In my pom-file I declare JUnit 4.8.2 as the only dependency. Still Maven insists on using JUnit version 3.8.1. How do I fix it?

The problem manifests itself in a compilation failure: "package org.junit does not exist". This is because of the import statement in my source code. The correct package name in JUnit 4.* is org.junit.* while in version 3.* it is junit.framework.*

I think I have found documentation on the root of the problem on http://maven.apache.org/plugins/maven-surefire-plugin/examples/junit.html but the advice there seems to be meant for Maven experts. I did not understand what to do.

Answer

Renato Lochetti picture Renato Lochetti · Jul 11, 2013

Just to have an answer with the complete solution to help the visitors:

All you need to do is add the junit dependency to pom.xml. Don't forget the <scope>test</scope>

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.11</version>
  <scope>test</scope>
</dependency>