Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven compile gives: Cannot find symbol - For a class sitting in the same app

It's a while since I met such puzzling issue. I'm having a class that references another one sitting in another package in the same application, that is, NOT in another jar archive file.

The including class is learnintouch-rest/src/test/java/com/thalasoft/learnintouch/rest/acceptance/AbstractControllerTest.java

The included class is /home/stephane/dev/java/projects/learnintouch-rest/src/test/java/com/thalasoft/learnintouch/rest/config/WebTestConfiguration.java

Under Eclipse there is no issue and no compilation error in the editor.

But running a Maven build gives a compilation error:

mvn clean test-compile -Pacceptance

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:testCompile (default-testCompile) on project learnintouch-rest: Compilation failure: Compilation failure:
[ERROR] /home/stephane/dev/java/projects/learnintouch-rest/src/test/java/com/thalasoft/learnintouch/rest/acceptance/AbstractControllerTest.java:[16,46] cannot find symbol
[ERROR] symbol:   class WebTestConfiguration
[ERROR] location: package com.thalasoft.learnintouch.rest.config
[ERROR] /home/stephane/dev/java/projects/learnintouch-rest/src/test/java/com/thalasoft/learnintouch/rest/acceptance/AbstractControllerTest.java:[21,116] cannot find symbol
[ERROR] symbol: class WebTestConfiguration

Here is the code of the including class:

@RunWith(SpringJUnit4ClassRunner.class)
@Transactional
@WebAppConfiguration
@ContextConfiguration(classes = {
    ApplicationConfiguration.class,
    WebSecurityConfig.class,
    WebConfiguration.class,
    WebTestConfiguration.class
})
public abstract class AbstractControllerTest {

This abstract test class is sitting under the acceptance test directory, which mandates the explicit activation of the -Pacceptance profile when running the Maven command.

The default profile does not run this acceptance test, but only some integration test.

One thing to note, is that this abstract class looks like the one abstract class used in the integration test.

Here is the including class of the integration test:

import com.thalasoft.learnintouch.rest.config.WebTestConfiguration;

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = {ApplicationConfiguration.class},
        WebSecurityConfig.class,
        WebConfiguration.class,
        WebTestConfiguration.class
        })
@Transactional

public abstract class AbstractControllerTest {

As you can see, it looks much like the other one.

I can also give the pom.xml file content, if it can help:

<profiles>
    <profile>
        <id>rest</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <properties>
            <test.source.dir>src/test/java/com/thalasoft/learnintouch/rest</test.source.dir>
        </properties>
    </profile>
    <profile>
        <id>acceptance</id>
        <activation>
            <activeByDefault>false</activeByDefault>
        </activation>
        <properties>
            <test.source.dir>src/test/java/com/thalasoft/learnintouch/rest/acceptance</test.source.dir>
        </properties>
    </profile>
</profiles>

If you have any clue on this one, it'd be greatly appreciated.

like image 645
Stephane Avatar asked Apr 27 '14 19:04

Stephane


2 Answers

With maven your tests can access to all the source code of your project (src/main/java) and all the test source code (default is src/test/java).

Here, your profile rest defines the test source directory as src/test/java/com/thalasoft/learnintouch/rest So, your test code can access everything in src/main/java and everything in src/test/java/com/thalasoft/learnintouch/rest

Your profile acceptance defines the test source directory as src/test/java/com/thalasoft/learnintouch/rest/acceptance So, your test code can access everything in src/main/java and everything in src/test/java/com/thalasoft/learnintouch/rest/acceptance. WebTestConfiguration is not accessible since it's in a package above.

To run specific tests with different profiles, I recommend to configure the surefire plugin in charge of running tests. A good example is available here : https://weblogs.java.net/blog/carcassi/archive/2011/04/21/running-integration-tests-and-unit-tests-separately-maven

like image 106
Melou Avatar answered Oct 22 '22 16:10

Melou


Problem Description:

I had similar problem with my project with below structure.

project
-pom.xml

-common-module  
--src, pom.xml, etc

-web-module  
--src, pom.xml, etc (but dependent on 'common')

mvn install would run fine for common-module but gives compilation error for web-module code that uses java class from common-module (cannot find symbol). I was using JDK 8 + Maven 3.2.5, but in pom compiler lever is set to 7.

MY Solution:

I have installed JDK 7 and Maven 3.1.1 and did a mvn install. Bingo..! Every with worked fine & Build Successful.

I couldn't find a solution for my problem any where, so though of posting my fix here as it is related. (may be it could help some one)

like image 1
vijay Avatar answered Oct 22 '22 18:10

vijay