Krummy
Krummy

Reputation: 668

maven error - package org.junit does not exist

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>

Upvotes: 1

Views: 8601

Answers (1)

Master Slave
Master Slave

Reputation: 28529

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

Upvotes: 3

Related Questions