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

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

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. Dont forget the <scope>test</scope>

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

@Dennis Roberts: You were absolutely right: My test class was located in src/main/java. Also the value of the scope element in the POM for JUnit was test, although that is how it is supposed to be. The problem was that I had been sloppy when creating the test class in Eclipse, resulting in it being created in src/main/java insted of src/test/java. This became easier to see in Eclipses Project Explorer view after running mvn eclipse:eclipse, but your comment was what made me see it first. Thanks.

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

my problem was a line inside my pom.xml i had the line <sourceDirectory>${basedir}/src</sourceDirectory> removing this line made maven use regular structure folders which solves my issue

Leave a Reply

Your email address will not be published.