[java] maven error: package org.junit does not exist

I'm trying to create the javadoc with maven and it fails. It also fails when doing the verify.

mvn verify

I get the following error:

(...)
    [INFO] -------------------------------------------------------------
    [ERROR] COMPILATION ERROR : 
    [INFO] -------------------------------------------------------------
    [ERROR] /home/miquel/creaveu/createOmegaMatrix/src/main/java/edu/url/salle/gtm/hnm/dataStructures/HFrame.java:[6,23]
package org.junit does not exist
    [ERROR] /home/miquel/creaveu/createOmegaMatrix/src/main/java/edu/url/salle/gtm/hnm/dataStructures/HFrame.java:[6,0]
static import only from classes and interfaces
    (ยทยทยท)

In my pom.xml file I have the following lines:

<dependency>
  <groupId>org.junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.8.2</version>
  <scope>test</scope>
</dependency>

and my local repository contains the junit jar file:

miquel@ubuntu:~/creaveu/createOmegaMatrix$ ls -l /home/miquel/.m2/repository/org/junit/junit/4.8.2/
total 248
**-rw-r--r-- 1 miquel miquel 237344 2012-09-13 11:01 junit-4.8.2.jar**
-rw-r--r-- 1 miquel miquel    236 2012-09-13 11:13 junit-4.8.2-javadoc.jar.lastUpdated
-rw-r--r-- 1 miquel miquel      0 2012-09-13 11:13 junit-4.8.2-javadoc.jar-not-available
-rw-r--r-- 1 miquel miquel    458 2012-09-12 18:35 junit-4.8.2.pom
-rw-r--r-- 1 miquel miquel    236 2012-09-13 11:13 junit-4.8.2-sources.jar.lastUpdated
-rw-r--r-- 1 miquel miquel      0 2012-09-13 11:13 junit-4.8.2-sources.jar-not-available
-rw-r--r-- 1 miquel miquel    163 2012-09-13 11:22 _maven.repositories
miquel@ubuntu:~/creaveu/createOmegaMatrix$

The code is fine because in my laptop, which I have no access now, I van run:

mvn javadoc:javadoc
mvn verify

with no problems, and also the tests work in eclipse IDE.

This question is related to java maven-3 junit4

The answer is


if you are using Eclipse watch your POM dependencies and your Eclipse buildpath dependency on junit

if you select use Junit4 eclipse create TestCase using org.junit package but your POM use by default Junit3 (junit.framework package) that is the cause, like this picture:

see JUNIT conflict

Just update your Junit dependency in your POM file to Junit4 or your Eclipse BuildPath to Junit3


I fixed this error by inserting these lines of code:

<dependency>
  <groupId>junit</groupId>     <!-- NOT org.junit here -->
  <artifactId>junit-dep</artifactId>
  <version>4.8.2</version>
  <scope>test</scope>
</dependency>

into <dependencies> node.

more details refer to: http://mvnrepository.com/artifact/junit/junit-dep/4.8.2


In my case, the culprit was not distinguish the main and test sources folder within pom.xml (generated by eclipse maven project)

<build>
    <sourceDirectory>src</sourceDirectory>
    ....
</build>

If you override default source folder settings in pom file, you must explicitly set the main AND test source folders!!!!

<build>
    <sourceDirectory>src/main/java</sourceDirectory>
    <testSourceDirectory>src/test/java</testSourceDirectory>
    ....
</build>

Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to maven-3

Failed to read artifact descriptor for org.apache.maven.plugins:maven-source-plugin:jar:2.4 Specifying java version in maven - differences between properties and compiler plugin m2e error in MavenArchiver.getManifest() How do I force Maven to use my local repository rather than going out to remote repos to retrieve artifacts? Plugin org.apache.maven.plugins:maven-clean-plugin:2.5 or one of its dependencies could not be resolved MAVEN_HOME, MVN_HOME or M2_HOME Cannot load properties file from resources directory 'mvn' is not recognized as an internal or external command, operable program or batch file 'dependencies.dependency.version' is missing error, but version is managed in parent Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile)

Examples related to junit4

Mockito: Mock private field initialization java.lang.Exception: No runnable methods exception in running JUnits How to write JUnit test with Spring Autowire? How to run JUnit tests with Gradle? Difference between @Before, @BeforeClass, @BeforeEach and @BeforeAll How to test that no exception is thrown? Failed to load ApplicationContext for JUnit test of Spring controller How does Junit @Rule work? maven error: package org.junit does not exist How do I assert an Iterable contains elements with a certain property?