Reputation: 1245
I am having a very simple junit test class that is running successfully with Eclipse JUnit launcher.
Below is the pom dependency for JUnit.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
...
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
...
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
</plugin>
</plugins>
</build>
</project>
But, on running maven build, I am getting below error message:
MyTest.java:[3,24] package org.junit does not exist
And, build runs successfully after removing <scope>test</scope>
from the junit dependency.
Why it is failing with test scope and how to fix it?
I am using Java 8 and Maven 3.3.9 versions.
EDIT - Minimal, Complete, and Verifiable code added
Since I cannot share the actual code, I have created a demo test class and got the same error below.
/MyProject/demo/src/test/java/com/MyTest.java:[3,24] package org.junit does not exist
/MyProject/demo/src/test/java/com/MyTest.java:[3,1] static import only from classes and interfaces
/MyProject/demo/src/test/java/com/MyTest.java:[5,17] package org.junit does not exist
MyTest class:
package com;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class MyTest {
@Test
public void demo() throws Exception {
int expected = 10;
int actual = 10;
assertEquals(expected, actual);
}
}
Also, it is a multi-module project. JUnit dependency is under common module and Test class is under demo module.
pom.xml in demo module to include common module dependencies:
<dependencies>
<dependency>
<groupId>myproject</groupId>
<artifactId>common</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
However, build got successful after copying MyTest class to common module. So, it seems like junit dependencies in common module are not importing to demo module.
Upvotes: 1
Views: 9459
Reputation: 15316
In Maven not all the transitive dependencies (dependencies of your dependency) are added to the classpath - it depends on the scope. If the transitive dependency is test
or provided
scoped then they are never added to the classpath. It makes sense since if you add a lib to your dependencies you don't want to depend on its test libs.
See Maven's Introduction to the Dependency Mechanism#Dependecy Scope, especially the table at the bottom of the section.
So the solution is to add junit dependency to your target module (not to your common module). Or to add it to the parent (this is a bit hacky since not every module may need this dependency) which would effectively add it to all the children.
Upvotes: 3