-2

I am using a simple java application with unit and integration tests.

My goal is to run integration tests with maven & failsafe plugin.

I am having a problem running my integration tests. short explenation: I get an error from "test-compile" phase at maven, which says "cannot find symbol".

project structure:

  • parent (pom.xml)
    • integration test module (test files with pom.xml)
    • webApp module (source files with pom.xml)

the error I am getting is becouse I am trying to instantiate an object from webApp module (inside some test - in integration tests module). I also added dependency for webapp inside integration-test module pom.

details:

the integration test class:

import junit.framework.TestCase;
import org.junit.Test;

public class CalcsITCase extends TestCase {
@Test
public void emptyTest() throws Exception {
    assertEquals(true,true);
}
@Test
public void playTest() throws Exception {
    Band band = new Band(4);
    assertEquals(true,true);
}

}

the class that being tested

import org.json.JSONObject;

import java.security.InvalidParameterException;

public class Band {
    public int id;
    public String name = "";
    public String logo = "";
    public String song = "";
    public int votes = 0;

    public Band(int members) {
        System.out.println(members + " members in band");
    }
....

The integration test pom.xml:

<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/maven-v4_0_0.xsd">
    <parent>
        <artifactId>app-all</artifactId>
        <groupId>discover-demo-app</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>integration-tests</artifactId>
    <packaging>jar</packaging>
    <name>integration-tests Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>discover-demo-app</groupId>
            <artifactId>webapp</artifactId>
            <version>1.0-SNAPSHOT</version>
            <type>war</type>
<!--            <scope>test</scope>-->
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.jayway.restassured</groupId>
            <artifactId>rest-assured</artifactId>
            <version>2.4.0</version>
        </dependency>
    </dependencies>
    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.5.1</version>
                    <configuration>
                        <!-- put your configurations here -->
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.12.4</version>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
        </plugins>
        <finalName>integration-tests</finalName>
    </build>
    <profiles>
        <profile>
            <id>it</id>
            <build>
                <plugins>
                    <!--added for integration tests-->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-failsafe-plugin</artifactId>
                        <version>2.19.1</version>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>integration-test</goal>
                                    <goal>verify</goal>
                                </goals>
                            </execution>
                        </executions>
                        <configuration>
                            <skipTests>false</skipTests>
                            <properties>
                                <property>
                                    <name>listener</name>
                                    <value>....</value>
                                </property>
                            </properties>
                        </configuration>
                    </plugin>
                </plugins>
            </build>

            <dependencies>
                <dependency>
                    <groupId>....</groupId>
                    <artifactId>....</artifactId>
                    <version>12.53.1-SNAPSHOT</version>
                </dependency>
            </dependencies>
        </profile>
    </profiles>
</project>

the webApp pom.xml

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>discover-demo-app</groupId>
    <artifactId>webapp</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>

    <name>Discover Demo App - Tribute to Progressive Music (Web App)</name>

    <dependencies>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-server</artifactId>
            <version>9.2.5.v20141112</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-util</artifactId>
            <version>9.2.5.v20141112</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>

        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20140107</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>webapp</finalName>
        <plugins>
            <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.7.5.201505241946</version>
        <configuration>
          <output>file</output>
          <append>true</append>
        </configuration>
        <executions>
          <execution>
            <id>jacoco-initialize</id>
            <goals>
              <goal>prepare-agent</goal>
            </goals>
          </execution>
          <execution>
            <id>jacoco-site</id>
            <phase>verify</phase>
            <goals>
              <goal>report</goal>
            </goals>
          </execution>
        </executions>
        </plugin>
        </plugins>
    </build>
    <profiles>
    <profile>
      <id>A</id>
      <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <!-- Minimal supported version is 2.4 -->
            <version>2.13</version>
            <configuration>
                <skipTests>true</skipTests>
              <properties>
                <property>
                  <name>listener</name>
                  <value>...</value>
                </property>
              </properties>
            </configuration>
          </plugin>
        </plugins>
      </build>

      <dependencies>
                <dependency>
                    <groupId>...</groupId>
                    <artifactId>...</artifactId>
                    <version>12.53.1-SNAPSHOT</version>
                </dependency>
      </dependencies>
    </profile>

        <profile>
            <id>it</id>
            <build>
                <plugins>
                    <!--added for integration tests-->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-failsafe-plugin</artifactId>
                        <version>2.19.1</version>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>integration-test</goal>
                                    <goal>verify</goal>
                                </goals>
                            </execution>
                        </executions>
                        <configuration>
                            <skipTests>false</skipTests>
                            <properties>
                                <property>
                                    <name>listener</name>
                                    <value>...</value>
                                </property>
                            </properties>
                        </configuration>
                    </plugin>
                </plugins>
            </build>

            <dependencies>
                <dependency>
                    <groupId>...</groupId>
                    <artifactId>...</artifactId>
                    <version>12.53.1-SNAPSHOT</version>
                </dependency>
            </dependencies>
        </profile>
        </profiles>
</project>

the error i am getting from maven (when running the following command inside integration test) is:

mvn verify -Dmaven.test.failure.ignore=true

[INFO] ------------------------------------------------------------------------                                  
[INFO] Building integration-tests Maven Webapp 1.0-SNAPSHOT                                                      
[INFO] ------------------------------------------------------------------------                                  
[INFO]                                                                                                           
[INFO] --- maven-resources-plugin:2.5:resources (default-resources) @ integration-tests ---                      
[debug] execute contextualize                                                                                    
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!

[INFO] skip non existing resourceDirectory C:\Users\..\workspace\..\integration-tests\src\main\re
sources                                                                                                          
[INFO]                                                                                                           
[INFO] --- maven-compiler-plugin:3.5.1:compile (default-compile) @ integration-tests ---                         
[INFO] No sources to compile                                                                                     
[INFO]                                                                                                           
[INFO] --- maven-resources-plugin:2.5:testResources (default-testResources) @ integration-tests ---              
[debug] execute contextualize                                                                                    
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!

[INFO] skip non existing resourceDirectory C:\Users\..\workspace\..-demo-app\integration-tests\src\test\re
sources                                                                                                          
[INFO]                                                                                                           
[INFO] --- maven-compiler-plugin:3.5.1:testCompile (default-testCompile) @ integration-tests ---                 
[INFO] Changes detected - recompiling the module!                                                                
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent!      
[INFO] Compiling 3 source files to C:\Users\..\workspace\..e-demo-app\integration-tests\target\test-classes

[INFO] /C:/Users/../workspace/..-demo-app/integration-tests/src/test/java/com/../devops/demoapp/RestServle
tTest.java: C:\Users\..\workspace\..-demo-app\integration-tests\src\test\java\com\..\devops\demoapp\RestSe
rvletTest.java uses unchecked or unsafe operations.                                                              
[INFO] /C:/Users/../workspace/..-demo-app/integration-tests/src/test/java/com/../devops/demoapp/RestServle
tTest.java: Recompile with -Xlint:unchecked for details.                                                         
[INFO] -------------------------------------------------------------                                             
[ERROR] COMPILATION ERROR :                                                                                      
[INFO] -------------------------------------------------------------                                             
[ERROR] /C:/Users/../workspace/..-demo-app/integration-tests/src/test/java/com/../devops/demoapp/CalcsITCa
se.java:[20,9] cannot find symbol                                                                                
  symbol:   class Band                                                                                           
  location: class com....devops.demoapp.CalcsITCase                                                              
[ERROR] /C:/Users/../workspace/..-demo-app/integration-tests/src/test/java/com/../devops/demoapp/CalcsITCa
se.java:[20,25] cannot find symbol                                                                               
  symbol:   class Band                                                                                           
  location: class com....devops.demoapp.CalcsITCase                                                              
[INFO] 2 errors                                                                                                  
[INFO] -------------------------------------------------------------                                             
[INFO] ------------------------------------------------------------------------                                  
[INFO] BUILD FAILURE                                                                                             
[INFO] ------------------------------------------------------------------------                                  
[INFO] Total time: 2.676s                                                                                        
[INFO] Finished at: Tue Mar 01 11:45:17 IST 2016                                                                 
[INFO] Final Memory: 16M/304M                                                                                    
[INFO] ------------------------------------------------------------------------                                  
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.5.1:testCompile (default-testComp
ile) on project integration-tests: Compilation failure: Compilation failure:                                     
[ERROR] /C:/Users/../workspace/..-demo-app/integration-tests/src/test/java/com/../devops/demoapp/CalcsITCa
se.java:[20,9] cannot find symbol                                                                                
[ERROR] symbol:   class Band                                                                                     
[ERROR] location: class com....devops.demoapp.CalcsITCase                                                        
[ERROR] /C:/Users/../workspace/..-demo-app/integration-tests/src/test/java/com/../devops/demoapp/CalcsITCa
se.java:[20,25] cannot find symbol                                                                               
[ERROR] symbol:   class Band                                                                                     
[ERROR] location: class com.(..).devops.demoapp.CalcsITCase                                                        
[ERROR] -> [Help 1]                                                                                              
[ERROR]                                                                                                          
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.                              
[ERROR] Re-run Maven using the -X switch to enable full debug logging.                                           

Edit:

Additional Info:

  • The maven output states that we can't compile the "CalcsITCase.java" class because we doesn't know "Band.java" class.. integration-test module does compile in intellij, it doesn't compile in maven..
  • The webApp module compile in maven and in intellij successfully.

Also, the test class (CalcsITCase) and subject class (Band) are in the same package.. so it's not an import issue..

i would bet on a maven issue.. but after hours of googling and searching.. i could not found it..

user3250354
  • 101
  • 10
  • You code does not correctly compile...First i would suggest to fix this first... – khmarbaise Mar 01 '16 at 10:00
  • my code compile.. i only replaced the names with "..." in a caple of places.. – user3250354 Mar 01 '16 at 10:25
  • Sorry but the output in your post states something different.... – khmarbaise Mar 01 '16 at 10:34
  • the maven output states that we can't compile the "CalcsITCase.java" class becouse we doesn't know Band.java class.. the code (integration-test module) does compile in intellij, it doesn't compile in maven.. the webApp module compile in maven and in intellij. – user3250354 Mar 01 '16 at 10:38
  • 1
    Have you correctly checked that the Test class is in `src/test/java` and not in `src/main/java` ...Furthermore if you like to have the integration in a separate folder you need to add the folder by using build-helper-maven-plugin. I would recommend you to move the integration test classes into `src/test/java` location. They are being separated by the naming schema... – khmarbaise Mar 01 '16 at 12:56
  • yes, the Test class is in "src/test/java". can you elaborate about the "build-helper-maven-plugin" ? how does it help me ? the integration test is in a different module.. not just a folder.. in my case.. – user3250354 Mar 02 '16 at 16:38
  • So the folder `integration-tests` is a module name ? – khmarbaise Mar 02 '16 at 18:37
  • yes. its a folder and a module name. this module is sibling to the webApp module which is being tested with the integration tests that are in "integration-test" module.. – user3250354 Mar 02 '16 at 20:03

1 Answers1

0

You must import the Band class in your test class, i.e. add the following line to the list import statements in CalcsITCase:

import replace.with.correct.package.name.Band;

Obviously, you need to change the replace.with.correct.package.name with the package name of the Band class.

matsev
  • 32,104
  • 16
  • 121
  • 156
  • it does know Band. it has the same package name.. i don't have any compilation error in intellij.. – user3250354 Mar 01 '16 at 10:24
  • Well perhaps that is the intention, but the output that you provided states something else: `COMPILATION ERROR [...] cannot find symbol [...] symbol: class Band`. I suggest that you double check that the package name of the `Band` class and the `CalcsITCase` match. – matsev Mar 01 '16 at 10:28
  • first of all, i appretiate your help. second of all, already done that.. the package is the same.. i have updated the post after your comment.. as you can see, everything works fine from intellij.. i can run the test, and i can see i know Band.. – user3250354 Mar 01 '16 at 10:36