集成测试与故障安全

时间:2016-03-01 09:51:03

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

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

我的目标是使用maven& amp;故障安全插件。

我在运行集成测试时遇到问题。 short explenation:我在maven的“test-compile”阶段得到一个错误,它说“找不到符号”。

项目结构:

  • parent(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 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.                                           

编辑:

其他信息:

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

此外,测试类(CalcsITCase)和主题类(Band)在同一个包中..因此它不是导入问题..

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

1 个答案:

答案 0 :(得分:0)

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

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

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