Problem with Maven 3 and JUnit 4: org.junit package does not exist

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

The problem is 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. * So far in version 3. * this is junit.framework. *

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

+53
maven junit junit4 maven-3 junit3
May 01 '11 at 2:25 a.m.
source share
14 answers

@Dennis Roberts: You were absolutely right: my test class was in src / main / java. Also, the value of the "scope" element in the POM for JUnit was "test", although it should be so. The problem was that I was sloppy when creating a test class in Eclipse, as a result of which it was created in src / main / java insted from src / test / java. It became easier to see in the Project Eclipse view of Project Explorer after running "mvn eclipse: eclipse", but your comment was what made me see it first. Thank.

+29
May 16 '11 at 9:47 a.m.
source share

Just to get an answer with a complete solution to help visitors:

All you have to do is add the junit dependency to pom.xml . Do not forget <scope>test</scope>

 <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> 
+32
Jul 11 '13 at 18:30
source share

my problem was in my pom.xml line, I had the line <sourceDirectory>${basedir}/src</sourceDirectory> this line due to which maven used regular structure folders that solve my problem

+15
Mar 06 '14 at 22:11
source share

remove tag area in pom.xml for junit to work.

+9
Jan 19 '15 at 6:13
source share

How did you announce the version?

 <version>4.8.2</version> 

Remember the meaning from this declaration, explained here (see NOTES) :

When declaring a "normal" version, such as 3.8.2 for Junit, internally it appears as "allow anything, but prefers 3.8.2." This means that when a conflict is detected, Maven allows you to use conflict algorithms to select the best version. If you specify [3.8.2], this means that only 3.8.2 will be used and nothing else.

To force version 4.8.2, try

 <version>[4.8.2]</version> 

Since you have no other dependencies in your project, there should be no conflicts causing your problem. The first ad should work for you if you can get this version from the repository. Do you inherit dependencies from the parent pom?

+4
May 04 '11 at 19:41
source share

I had the same problem. All I did was: from the pom.xml file, I removed the dependency for junit 3.8 and added a new dependency for junit 4.8. Then I did maven clean and maven install. It did the trick. To check, after installing maven, I sent the dependencies project-> properties-build path-> maven and saw that now the junit 3.8 jar is gone !, and the junit 4.8 jar is specified. cool!!. Now my test passes like a charm. Hope this helps somehow ...

+3
Sep 29 '12 at 13:28
source share

Add this dependency to your pom.xml file:

http://mvnrepository.com/artifact/junit/junit-dep/4.8.2

 <!-- https://mvnrepository.com/artifact/junit/junit-dep --> <dependency> <groupId>junit</groupId> <artifactId>junit-dep</artifactId> <version>4.8.2</version> </dependency> 
+3
Aug 02 '13 at
source share

I had a similar problem where Eclipse compiled my code just fine, but Maven failed to compile the tests every time, even though JUnit was on my dependency list and the tests were in / src / test / java /.

In my case, I had the wrong version of JUnit in my dependency list. I wrote JUnit4 tests (with annotations), but had JUnit 3.8.x as my dependency. Between version 3.8.x and 4 from JUnit, they changed the package name from junit.framework to org.junit, so Maven is still breaking compilation using the JUnit jar.

I'm still not quite sure why Eclipse has compiled successfully. It should have its own copy of JUnit4 somewhere in the classpath. Hope this alternative solution is helpful to people. I reached this solution after I said Arthur's link above.

+1
Sep 26 '11 at 13:05
source share

I also ran into this problem - I tried to pull an object from the source, and it worked in the test code, but not in the src code. To continue testing, I copied a block of code from the test and dropped it into src code, and then immediately deleted the JUnit lines, so I just used the way to test the object. Then all of a sudden my code will not compile.
The problem was that when I deleted the code, Eclipse successfully resolved all the classes, so I had JUnit calls coming from my src code, which was wrong. I should have noticed warnings at the top about unused imports, but I did not see them.
As soon as I removed the unused JUnit import into my src file, all this worked perfectly.

+1
Jan 23 '17 at 20:34 on
source share

I had the files in the right places, and just removing <scope>test</scope> from the JUnit dependency record solved the problem (I am using JUnit 4.12). I believe that with the scope of test dependency was simply ignored at compile time. Now everything works, even when I call mvn test .

+1
Feb 22 '17 at 18:48
source share

My business was a simple oversight.

I place the JUnit dependency declaration inside the <dependencies> under the <dependencyManagement/> node instead of the <project/> in the POM file. The right way:

 <project> <!-- Other elements --> <dependencies> <!-- Other dependencies--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> </dependency> </dependencies> <project> 
+1
Jun 13 '17 at 16:32
source share

I had a pretty similar problem in the test-utils project (adding functions, rules and statements to JUnit) of a child element dependent on the parent project. The class dependent on the org.junit.rules package was in src / main / java.

So, I added junit dependency without a test area and solved the problem:

pom.xml test use project:

 <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> 

pom.xml of the parent project:

 <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> 
0
Nov 21 '17 at 10:21
source share

I also had the same problem as shown below.

enter image description here

To fix the problem, the lines below are added to the dependencies section at the build.gradle application level.

 compile 'junit:junit:4.12' androidTestCompile 'com.android.support.test:runner:0.5' 

Gradle then reports the following warning.

 Warning:Conflict with dependency 'com.android.support:support-annotations'. Resolved versions for app (25.1.0) and test app (23.1.1) differ. See http://g.co/androidstudio/app-test-app-conflict for details. 

To resolve this warning, the next section is added to the build.gradle application level.

 configurations.all { resolutionStrategy { force 'com.android.support:support-annotations:23.1.1' } } 
0
Dec 09 '17 at 10:35
source share

By default, maven scans these folders for the java and test classes respectively - src / main / java and src / test / java

When src is specified with test classes in the source and the junit dependency area in pom.xml is referred to as test - org.unit maven will not be found.

-2
Oct 23 '15 at 6:20
source share



All Articles