带故障安全的集成测试



我正在使用一个简单的Java应用程序,其中包含单元和集成测试。

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

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

项目结构:

  • 父项(绒球.xml)
    • 集成测试模块(带POM的测试文件.xml)
    • 网络应用模块(带有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>

网络应用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 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-testssrcmainre
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-appintegration-testssrctestre
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-appintegration-teststargettest-classes
[INFO] /C:/Users/../workspace/..-demo-app/integration-tests/src/test/java/com/../devops/demoapp/RestServle
tTest.java: C:Users..workspace..-demo-appintegration-testssrctestjavacom..devopsdemoappRestSe
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问题......但经过几个小时的谷歌搜索和搜索......我找不到它。

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

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

显然,您需要使用 Band 类的包名称更改replace.with.correct.package.name

最新更新