Maven test compile does not find class in classpath

Florian Schaetz picture Florian Schaetz · Nov 26, 2015 · Viewed 7.1k times · Source

I've got a little project with this pom...

<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">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example.shipcloud</groupId>
    <artifactId>shipcloud-api</artifactId>
    <description>The Java API for ShipCloud.</description>
    <version>1.0-SNAPSHOT</version>
    <prerequisites>
        <maven>3.0</maven>
    </prerequisites>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.compiler.source.version>1.8</project.compiler.source.version>
        <project.compiler.target.version>1.8</project.compiler.target.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.2.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.6.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.4</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <!-- More Matchers than the default version in JUnit -->
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-all</artifactId>
            <version>1.3</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.2.0.RELEASE</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.assertj</groupId>
            <artifactId>assertj-core</artifactId>
            <version>2.2.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.jayway.jsonpath</groupId>
            <artifactId>json-path</artifactId>
            <version>2.0.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

In Eclipse, everything works fine, the tests work (using that class that maven cannot find), but when I try to clean install this via maven, I get an error that it cannot find AbstractCharSequenceAssert (part of AssertJ) in this line (which is a class below another public class):

class UrlParameterStringAssert extends AbstractCharSequenceAssert<UrlParameterStringAssert, String> {

INFO] ------------------------------------------------------------- [ERROR] COMPILATION ERROR :
[INFO] ------------------------------------------------------------ [ERROR] /home/fschaetz/workspaces/workspace5/.../shipcloud-api/src/test/java/com/example/shipcloud/rest/shipcloud/domain/AddressSearchFieldsTest.java:[103,39] error: cannot find symbol [ERROR] symbol: class AbstractCharSequenceAssert

I have looked through the debug output and when running the testCompile, the AssertJ jar is actually on the classpath given to the compiler:

-classpath ...:/home/fschaetz/.m2/repository/org/assertj/assertj-core/2.2.0/assertj-core-2.2.0.jar:...

(The file exists, is readable, contains the class and works fine in Eclipse).

I've tried clean (maven/eclipse), running an update on the project, etc. but nothing seems to work. Same thing happens when I try to run the maven install in jenkins, btw.

The imports of the class that makes trouble are...

import static de.assona.rest.shipcloud.domain.UrlParameterStringAssert.assertThat;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import org.assertj.core.api.AbstractCharSequenceAssert;
import org.junit.Before;
import org.junit.Test;

Any ideas?

Answer

Florian Schaetz picture Florian Schaetz · Nov 26, 2015

I honestly have no idea WHY the problem is what it is (and would be very glad for anyone to explain it), but what is WAS is that these two classes were defined in the same file...

public class AddressSearchFieldsTest {

... some testing ...

}

class UrlParameterStringAssert extends AbstractCharSequenceAssert<UrlParameterStringAssert, String> {

... a custom Assertion class...

}

Normally I don't expect troubles with something like this (as there should be no problem declaring a non-public class in the same file), but somehow maven didn't like it. Moving the UrlParameterStringAssert to its own file solved the problem. Would love to know why.