Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 946
Are Husby Avatar asked May 01 '11 02:05

Are Husby


People also ask

What is org JUnit Jupiter?

JUnit Jupiter is the combination of the programming model and extension model for writing tests and extensions in JUnit 5. The Jupiter sub-project provides a TestEngine for running Jupiter based tests on the platform. JUnit Vintage provides a TestEngine for running JUnit 3 and JUnit 4 based tests on the platform.

What is @RunWith in JUnit?

When a class is annotated with @RunWith or extends a class annotated with @RunWith , JUnit will invoke the class it references to run the tests in that class instead of the runner built into JUnit. We added this feature late in development.


1 Answers

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> 
like image 112
Renato Lochetti Avatar answered Dec 06 '22 02:12

Renato Lochetti