90

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.

abhi
  • 1,760
  • 1
  • 24
  • 40
theme
  • 1,097
  • 2
  • 9
  • 13

5 Answers5

179

Ok, you've declared junit dependency for test classes only (those that are in src/test/java but you're trying to use it in main classes (those that are in src/main/java).

Either do not use it in main classes, or remove <scope>test</scope>.

Andrew Logvinov
  • 21,181
  • 6
  • 52
  • 54
  • 2
    damn!!! right that solved the problem with verify goal. Now I have other problems with javadoc, but regarding Latex. So the problem is solved!!! Thanks @Andrew – theme Sep 13 '12 at 09:50
  • When I remove scope test from my POM file, my testcases never get executed though I don't get any error which I used to get before (same as mentioned above). My tests are also under src/test/java and I have few libs which are in src/main/java. – Paresh Jan 29 '16 at 17:00
  • 1
    You can scope a parent pom with test and have a child pom with compile if a child module uses junit when not in the test hierarchy. – Daniel Jul 27 '16 at 02:49
  • I'm wondering why I get this when using test scope for JUnit dependency: .../src/test/java/com/...RuleTest.java:[3,24] package org.junit does not exist – Panu Haaramo Oct 22 '17 at 12:29
  • Thanks! I was struggling with this problem for hours. – LearnerAllTheWay Jan 06 '22 at 11:03
  • Still have the same problem – fsalazar_sch Nov 02 '22 at 13:14
38

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

cellepo
  • 4,001
  • 2
  • 38
  • 57
Siwei
  • 19,858
  • 7
  • 75
  • 95
  • 1
    This is a bad solution as you left out the `test` tags in this dependency section. This means the tests will be compiled into your production code. – LightCC Dec 24 '18 at 00:15
15

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

Grubhart
  • 1,106
  • 13
  • 19
  • well, I asked this two years ago, and now I'm not working on it any more, at least now. But thanks for the answer. Don't know if I have to set it as closed somehow. – theme Jul 23 '14 at 16:07
  • 1
    @dwjohnston great!! I'm not working with maven, but glad to see the my question helps other ;) also thnx Grubhart for the answer. :) – theme Aug 03 '15 at 08:46
10

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>
nix
  • 744
  • 9
  • 16
  • Removing the source directory configuration worked for me. But your comment helped. – Karthik P Apr 29 '19 at 10:17
  • I found that if you use the standard locations of src/main/java and src/test/java that you can leave out both Directory settings. – Solx May 04 '22 at 19:38
0

I solved this by using a combination of some of the above tips and other ideas found elsewhere on Stackoverflow.

Get the currently most recent version of Junit via Maven, by adding this to my pom.xml

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-api</artifactId>
    <version>5.9.0-M1</version>
    <scope>test</scope>
</dependency>   

Specify different source directories for main code and test code in pom.xml

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

However, the most important change was that I had attempted to create some test methods in my main source code, which had this import statement.

import org.junit.jupiter.api.Test; 

As soon as I removed all test functionality from the main source files, it all started working properly again.

This results in neater code, because all tests are now clearly separated from the main build.

DAB
  • 1,631
  • 19
  • 25
  • 1
    Thank you, java + maven newbie here, needed to learn sourceDirectory + testSourceDirectory exist. – Hettomei Jan 27 '23 at 15:57