2

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?

Florian Schaetz
  • 10,454
  • 5
  • 32
  • 58
  • Could you post the imports of the class `AddressSearchFieldsTest`? – Tunaki Nov 26 '15 at 08:30
  • Is `UrlParameterStringAssert` your own class or provided by AssertJ? – M. Deinum Nov 26 '15 at 08:31
  • Mine. I'm extending the `AbstractCharSequenceAssert` to do some assertion magic of my own. – Florian Schaetz Nov 26 '15 at 08:31
  • Have you already had a look into the output of a maven debug run? `mvn -X clean compile` – SubOptimal Nov 26 '15 at 08:37
  • Yep, that's why I know that the asserj jar is actually on the (test compile) classpath (see the quote, it's directly cut from the maven debug output). Unfortunately, nothing I see there helps, it's just "doing test compile with these parameter... " "compile error". – Florian Schaetz Nov 26 '15 at 08:39
  • And for the sake of completeness: Removing `test` from the assertj dependency doesn't change anything, same error. – Florian Schaetz Nov 26 '15 at 08:44
  • Can you post in which goal you get the error. As normally a `mvn clean compile` would not compile any code in `src/test/...`. – SubOptimal Nov 26 '15 at 08:51
  • No I mean from the mvn log output something like `maven-compiler-plugin:2.3.2:testCompile` (post the whole line). As I wrote `mvn clean compile` doesn't compile any sources in `src/test/...`. – SubOptimal Nov 26 '15 at 09:01
  • [INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ shipcloud-api --- – Florian Schaetz Nov 26 '15 at 09:05

2 Answers2

0

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.

Florian Schaetz
  • 10,454
  • 5
  • 32
  • 58
  • Have a look to my answer. Seems you should dig deeper into your maven settings and check the debug output. – SubOptimal Nov 26 '15 at 11:22
  • I do not think that you have a Maven problem, but are just seeing symptoms of another problem depending on your particular Maven setup. Maybe [this answer](https://stackoverflow.com/a/2336762/1082681) can enlighten you. – kriegaex Mar 09 '22 at 00:40
0

For me you have a strange maven setting somehow.

Assume we have following structure

src\main\java\Foo.java                                               
src\test\java\sub\optimal\mavenexample\AddressSearchFieldsTest.java

Foo.java

class Foo { }

AddressSearchFieldsTest.java

import org.assertj.core.api.AbstractCharSequenceAssert;
public class AddressSearchFieldsTest { }

class UrlParameterStringAssert extends
    AbstractCharSequenceAssert<UrlParameterStringAssert, String> {
    UrlParameterStringAssert(String s) {
        super(s, UrlParameterStringAssert.class);
    }
}

using your pom.xml following goal will be executed by mvn clean compile

# from the log output
maven-compiler-plugin:2.3.2:compile (default-compile)

and produce the following class files

target\classes\Foo.class

the goal default-testCompile (as you mention) is executed for example by mvn clean compile test

# from the log output
maven-compiler-plugin:2.3.2:testCompile (default-testCompile)

and produce the following class files

target\classes\Foo.class
target\test-classes\AddressSearchFieldsTest.class
target\test-classes\UrlParameterStringAssert.class

You should check why your mvn clean compile tries to compile the test class files.

SubOptimal
  • 22,518
  • 3
  • 53
  • 69
  • 1
    As I wrote in the other comment, I don't do a `clean compile`. I do a `clean install`, which of course compiles (and runs) the test files. Sorry, I don't quite get the answer. I know why the test is trying to compile, no magic here (`install` will do that). What I want to know is, why it FAILS to do so. – Florian Schaetz Nov 26 '15 at 12:25
  • @FlorianSchaetz Sorry my mistake with the `mvn clean compile`. And yes, you're right, `install` will compile and execute the test classes. But even `mvn clean install` works out of the box with the parts in my answer. Do you get the same error when you try to run the `javac` command line (from the maven debug log) standalone? – SubOptimal Nov 26 '15 at 12:52