1

I got a little trouble using maven and junit. Running junit test on its own works perfect, but building with Maven mvn clean test fails. I made sure that all tests are within "src/test/java" and all sources are within "src/main/java", so junit can find them (regarding to other questions this was the problem in many other cases).

So does anybody knows why I this is not working for me?

Error Message snipped:

/D:/pathToProject/myProject/src/test/java/myPackage/MyFile.java:[5,17] package org.junit does not exist 

Pom.xml Snipped:

<...project information.../> <build> <sourceDirectory>src</sourceDirectory> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source /> <target /> </configuration> </plugin> </plugins> <plugins> </plugins> </build> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> </dependencies> </project> 
2
  • 3
    One wrong thing is: Why do you change the default location via <sourceDirectory>src</sourceDirectory> which does not make sense. Remove it. Furthermore i would suggest to use <source>1.5</source> and <target>1.5</target> at least or may be 1.6/1.7. Commented Jan 27, 2015 at 15:03
  • Thanks for your speedy answer. I already changed the source/target to 1.7, as lower versions were leading to other problems. I already thought of something like this and I will test it next week (since this is not really my project, but the one of my collegue -> he is at his vacations -> report the results next week) Commented Jan 27, 2015 at 16:08

1 Answer 1

3

Khmarbaise pointed you to a mistake, the fact that you've overwritten the src directory means that maven will try to compile your test classes in the compile phase, but would fail, as your jUnit dependency is given with the scope test and is available in test phase only which comes later

Either remove the scope 'test' or change your source directory location so that it doesn't include your test classes

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.