【问题标题】:Integration Test with failsafe带故障保护的集成测试
【发布时间】:2016-03-01 09:51:02
【问题描述】:

我正在使用带有单元和集成测试的简单 java 应用程序。

我的目标是使用 maven 和故障安全插件运行集成测试。

我在运行集成测试时遇到问题。 简短的解释:我在 maven 的“测试编译”阶段收到一个错误,上面写着“找不到符号”。

项目结构:

  • 父 (pom.xml)
    • 集成测试模块(带有 pom.xml 的测试文件)
    • webApp 模块(带有 pom.xml 的源文件)

我得到的错误是因为我试图从 webApp 模块实例化一个对象(在一些测试中 - 在集成测试模块中)。 我还在集成测试模块 pom 中添加了对 webapp 的依赖。

详情:

集成测试类:

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);
}

}

正在测试的类

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");
    }
....

集成测试 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>

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>

我从 maven 得到的错误(在集成测试中运行以下命令时)是:

mvn 验证 -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.                                           

编辑:

附加信息:

  • maven 输出表明我们无法编译“CalcsITCase.java”类,因为我们不知道“Band.java”类。 集成测试模块确实在 intellij 中编译,它不在 maven 中编译..
  • webApp 模块在 maven 和 intellij 中编译成功。

另外,测试类 (CalcsITCase) 和主题类 (Band) 在同一个包中.. 所以这不是导入问题..

我会打赌一个 maven 问题.. 但经过数小时的谷歌搜索和搜索.. 我找不到它..

【问题讨论】:

  • 你的代码没有正确编译...首先我建议先解决这个问题...
  • 我的代码编译..我只在很多地方用“...”替换了名称..
  • 抱歉,您的帖子中的输出说明有所不同......
  • maven 输出表明我们无法编译“CalcsITCase.java”类,因为我们不知道 Band.java 类。代码(集成测试模块)在 intellij 中编译,它不能在 maven 中编译。we​​bApp 模块在 maven 和 intellij 中编译。
  • 您是否正确检查了 Test 类在 src/test/java 而不是 src/main/java ...此外,如果您想将集成放在单独的文件夹中,您需要使用添加文件夹构建助手 Maven 插件。我建议您将集成测试类移动到src/test/java 位置。它们被命名模式分开......

标签: maven unit-testing integration-testing maven-failsafe-plugin


【解决方案1】:

您必须在您的测试类中import Band 类,即将以下行添加到CalcsITCase 中的列表导入语句中:

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

显然,您需要将replace.with.correct.package.name 更改为Band 类的包名。

【讨论】:

  • 它确实知道乐队。它具有相同的包名称..我在 intellij 中没有任何编译错误..
  • 嗯,也许这就是意图,但您提供的输出说明了其他内容:COMPILATION ERROR [...] cannot find symbol [...] symbol: class Band。我建议你仔细检查 Band 类的包名和 CalcsITCase 匹配。
  • 首先,感谢您的帮助。其次,已经这样做了.. 包是一样的.. 我在您发表评论后更新了帖子.. 如您所见,intellij 一切正常.. 我可以运行测试,我可以看到我知道乐队..
猜你喜欢
  • 1970-01-01
  • 2012-08-21
  • 2018-03-29
  • 1970-01-01
  • 2021-09-23
  • 2011-06-26
  • 1970-01-01
  • 2020-03-16
  • 1970-01-01
相关资源
最近更新 更多