Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用集成测试获得servlet的代码覆盖率_Java_Maven_Code Coverage_Jacoco Maven Plugin_Integration Testing - Fatal编程技术网

Java 如何使用集成测试获得servlet的代码覆盖率

Java 如何使用集成测试获得servlet的代码覆盖率,java,maven,code-coverage,jacoco-maven-plugin,integration-testing,Java,Maven,Code Coverage,Jacoco Maven Plugin,Integration Testing,我一直在尝试使用集成测试获得实际开发代码(servlet和其他)的代码覆盖率(代码覆盖率不适用于集成测试) 技术堆栈:Java 1.8、Maven 3.3.9和IntelliJ Idea 2018.3 我的项目结构如下 集成测试模块 Servlet模块(war) 单元1 单元2 为了运行集成测试(IT),我在IntelliJ中配置了Tomcat,使其在8081端口上运行。为了执行IT,我们一直在http://localhost:8081/rm-xsp/servletname 下面是我的pom

我一直在尝试使用集成测试获得实际开发代码(servlet和其他)的代码覆盖率(代码覆盖率不适用于集成测试)

技术堆栈:Java 1.8、Maven 3.3.9和IntelliJ Idea 2018.3

我的项目结构如下

  • 集成测试模块
  • Servlet模块(war)
  • 单元1
  • 单元2
为了运行集成测试(IT),我在IntelliJ中配置了Tomcat,使其在8081端口上运行。为了执行IT,我们一直在
http://localhost:8081/rm-xsp/servletname

下面是我的
pom
mvn clean verify-Pone
是我的maven目标

<properties>
<!--Servlet code-->
    <server.classes>../rm-xsp-war/target/classes</server.classes>
    <server.sources>../rm-xsp-war/src/main/java</server.sources>

    <!--Servlet dependency code-->
    <mms-main.classes>../rm-xsp-java/target/classes</mms-main.classes>
    <mms-main.sources>../rm-xsp-java/src/main/java</mms-main.sources>
</properties>

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.7.1</version>
            <configuration>
                <systemPropertyVariables>
                    <jacoco-agent.destfile>${basedir}/target/coverage-reports/jacoco-it.exec</jacoco-agent.destfile>
                </systemPropertyVariables>
                <parallel>methods</parallel>
                <threadCount>5</threadCount>

            </configuration>
            <executions>
                <execution>
                    <id>integration-test</id>
                    <goals>
                        <goal>integration-test</goal>
                    </goals>
                </execution>
                <execution>
                    <id>verify</id>
                    <goals>
                        <goal>verify</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

<plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.7.5.201505241946</version>
            <executions>
                <!-- prepare agent for measuring integration tests -->
                <execution>
                    <id>prepare-integration-tests</id>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                    <configuration>
                        <destFile>${basedir}/target/coverage-reports/jacoco-it.exec</destFile>
                        <propertyName>failsafeArgLine</propertyName>
                    </configuration>
                </execution>
                <execution>
                    <id>default-instrument</id>
                    <goals>
                        <goal>instrument</goal>
                    </goals>
                </execution>
                <execution>
                    <id>default-restore-instrumented-classes</id>
                    <phase>post-integration-test</phase>
                    <goals>
                        <goal>restore-instrumented-classes</goal>
                    </goals>
                </execution>
                <execution>
                    <id>default-report</id>
                    <phase>verify</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                    <configuration>
                        <dataFile>${basedir}/target/coverage-reports/jacoco-it.exec</dataFile>
                    </configuration>
                </execution>
                <execution>
                    <id>default-report-integration</id>
                    <goals>
                        <goal>report-integration</goal>
                    </goals>
                    <configuration>
                        <dataFile>${basedir}/target/coverage-reports/jacoco-it.exec</dataFile>
                    </configuration>
                </execution>
            </executions>
        </plugin>


<profiles>
    <profile>
        <id>one</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <version>1.7</version>
                    <executions>
                        <execution>
                            <id>post-test-report</id>
                            <phase>verify</phase>
                            <goals>
                                <goal>run</goal>
                            </goals>
                            <configuration>
                                <target>
                                    <taskdef name="report" classname="org.jacoco.ant.ReportTask">
                                        <classpath path="${basedir}/target/jacoco-jars/org.jacoco.ant.jar"/>
                                    </taskdef>
                                    <mkdir dir="${basedir}/target/coverage-report"/>
                                    <report>
                                        <executiondata>
                                            <fileset dir="${build.directory}">
                                                <include name="jacoco.exec"/>
                                            </fileset>
                                        </executiondata>
                                        <structure name="Integration Test Coverage Project">
                                            <group name="Server">
                                                <classfiles>
                                                    <fileset dir="${server.classes}"/>
                                                    <fileset dir="${mms-main.classes}"/>
                                                </classfiles>
                                                <sourcefiles encoding="UTF-8">
                                                    <fileset dir="${server.sources}"/>
                                                    <fileset dir="${mms-main.sources}"/>
                                                </sourcefiles>
                                            </group>
                                        </structure>
                                        <html destdir="${basedir}/target/coverage-report/html"/>
                                        <xml destfile="${basedir}/target/coverage-report/coverage-report.xml"/>
                                        <csv destfile="${basedir}/target/coverage-report/coverage-report.csv"/>
                                    </report>
                                </target>
                            </configuration>
                        </execution>
                    </executions>
                    <dependencies>
                        <dependency>
                            <groupId>org.jacoco</groupId>
                            <artifactId>org.jacoco.ant</artifactId>
                            <version>0.8.0</version>
                        </dependency>
                    </dependencies>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>
我好像错过了什么。我在同一个网站上浏览了很多帖子,但都没有找到解决办法


非常感谢您的任何建议。

工具的目标似乎不受任何阶段的约束(您是否看到它在maven日志中运行?)。尝试将其绑定到
预集成测试
@guido尝试过,但没有成功。我仍然认为代码覆盖率为“0”。我的电脑运行成功。请参考上面文章中我的测试执行输出。似乎您正在定义:
failsafeArgLine
,但是您没有将其设置为failsafe执行;尝试添加它:
${failsafeArgLine}
在maven failsafeplugint的
元素中添加它。我看到现在创建了
jacocoit.exec
。但是当报告进来时,我看到
[mkdir]创建的dir:C:\Users\WorkSpace\usage\u service\autoTestEnv\target\coverage report[report]正在加载执行数据文件C:\Users\WorkSpace\usage\u service\autoTestEnv\target\coverage reports\jacoco it.exec
消息。我觉得报告关注的是autoTestEnv文件夹的目标\类,而不是servlet文件夹。您在该位置找到文件的唯一原因是您在配置中设置了该文件;当jacoco对类进行仪器化时,它使用在target中收集的类
> [INFO] ---
> jacoco-maven-plugin:0.7.5.201505241946:restore-instrumented-classes
> (default-restore-instrumented-classes) @ autoTestEnv --- 
> [INFO] 
> [INFO] --- maven-failsafe-plugin:2.7.1:verify (verify) @ autoTestEnv
> --- [INFO] Failsafe report directory: C:\Users\workspace\autoTestEnv\target\failsafe-reports
>  [INFO]
> --- jacoco-maven-plugin:0.7.5.201505241946:report (default-report) @ autoTestEnv --- [INFO] Skipping JaCoCo execution due to missing
> execution data
> file:C:\Users\workspace\autoTestEnv\target\coverage-reports\jacoco-it.exec
> [INFO]  [INFO] ---
> jacoco-maven-plugin:0.7.5.201505241946:report-integration
> (default-report-integration) @ autoTestEnv --- [INFO] Skipping JaCoCo
> execution due to missing execution data
> file:C:\Users\workspace\autoTestEnv\target\coverage-reports\jacoco-it.exec
> [INFO]  [INFO] --- maven-antrun-plugin:1.7:run (post-test-report) @
> autoTestEnv --- [INFO] Executing tasks
> 
> main:
>     [mkdir] Created dir: C:\Users\workspace\autoTestEnv\target\coverage-report    [report]
> Writing bundle 'Server' with 155 classes