如何配置POM,以便我的应用服务器可以在Jacoco上运行?

时间:2019-08-12 19:59:02

标签: spring-boot jacoco-maven-plugin

我正在尝试将一个我不太熟悉的项目的一部分提取到自己的应用程序中,同时也将其从Spring更改为Spring Boot,而我现在正在努力的步骤是引入代码覆盖率。 Jacoco转储目标失败,因为它拒绝连接。我对Maven还是很陌生,不知道如何设置Jacoco目标,以便在Tomcat仍在进行集成测试时执行它们。

我尝试在exec-maven插件的某处添加其他执行,但是我真的不知道那是我应该做的。我主要从现有项目复制配置,因此在下面的XML中,某些注释可能不再适用。但是他们可能会阐明在非Boot项目中如何使用Cargo插件进行测试。

我尝试删除一些多余的配置,但这是我的属性:

<maven.jacoco.plugin.version>0.8.2</maven.jacoco.plugin.version>
        <jacoco.functional.test.exec.filename>${project.build.directory}/jacoco-it.exec</jacoco.functional.test.exec.filename>
        <jacoco.unit.test.instruction.coverage.min>75</jacoco.unit.test.instruction.coverage.min>
        <jacoco.func.test.instruction.coverage.min>75</jacoco.func.test.instruction.coverage.min>
    </properties>

Spring Boot依赖项包括spring-boot-starter-actuator,spring-boot-starter-web,spring-boot-devtools,spring-boot-starter-test和:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
    </dependency>

内部版本:

        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>pre-integration-test</id>
                        <goals>
                            <goal>start</goal>
                        </goals>
                        <configuration>
                            <profiles>
                                <profile>test</profile>
                            </profiles>
                        </configuration>
                    </execution>
                    <execution>
                        <id>post-integration-test</id>
                        <goals>
                            <goal>stop</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>


            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>${maven.jacoco.plugin.version}</version>
                <executions>
                    <execution>
                        <id>default-prepare-agent</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>default-prepare-agent-integration</id>
                        <!-- Setting <phase> to 'initialize' ensures the 'prepare-agent-integration' goal
                        is run before cargo is started. The <phase> really should be the default phase
                        (pre-integration-test) not 'initialize', to run right before integration-test starts
                        and not at the beginning of the build. The problem is that Cargo is being started before
                        this plugin even though this plugin is declared first. It's because maven does not follow
                        the order of plugins when one of them is inside <profile> -->
                        <goals>
                            <goal>prepare-agent-integration</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>default-report</id>
                        <goals>
                            <goal>report</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>target/jacoco-unit-test-coverage-reports</outputDirectory>
                        </configuration>
                    </execution>
                    <execution>
                        <id>default-report-integration</id>
                        <phase>verify</phase>
                        <goals>
                            <!-- dump jacoco-it.exec before we generate report, see
                            <execution><id>default-prepare-agent-integration</id>... above for more explanation -->
                            <goal>dump</goal>
                            <goal>report-integration</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>target/jacoco-functional-test-coverage-reports</outputDirectory>
                            <!-- must tell 'dump' where to dump, cause default is jacoco.exec and that
                            would overwrite the unit test's exec file -->
                            <destFile>${jacoco.functional.test.exec.filename}</destFile>
                        </configuration>
                    </execution>
                    <execution>
                        <id>default-check</id>
                        <goals>
                            <goal>check</goal>
                        </goals>
                        <configuration>
                            <rules>
                                <rule>
                                    <element>BUNDLE</element>
                                    <limits>
                                        <limit>
                                            <counter>INSTRUCTION</counter>
                                            <value>COVEREDRATIO</value>
                                            <minimum>${jacoco.unit.test.instruction.coverage.min}%</minimum>
                                        </limit>
                                    </limits>
                                </rule>
                            </rules>
                        </configuration>
                    </execution>
                    <execution>
                        <id>default-check-integration</id>
                        <goals>
                            <goal>check</goal>
                        </goals>
                        <configuration>
                            <dataFile>${jacoco.functional.test.exec.filename}</dataFile>
                            <rules>
                                <rule>
                                    <element>BUNDLE</element>
                                    <limits>
                                        <limit>
                                            <counter>INSTRUCTION</counter>
                                            <value>COVEREDRATIO</value>
                                            <minimum>${jacoco.func.test.instruction.coverage.min}%</minimum>
                                        </limit>
                                    </limits>
                                </rule>
                            </rules>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

和个人资料:

        <profile>
            <id>test</id>
            <properties>
                <activatedProperties>test</activatedProperties>
            </properties>
        </profile>
        <profile>
            <id>integration-test</id>
            <activation>
                <property>
                    <name>!maven.test.skip</name>
                </property>
            </activation>
            <properties>
                <activatedProperties>test</activatedProperties>
            </properties>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>exec-maven-plugin</artifactId>
                        <version>1.3.2</version>
                        <executions>
                            <execution>
                                <id>mock-tests</id>
                                <phase>integration-test</phase>
                                <goals>
                                    <goal>exec</goal>
                                </goals>
                                <configuration>
                                    <executable>python3</executable>
                                    <workingDirectory>${basedir}/src/test/functional</workingDirectory>
                                    <commandlineArgs>run.py mock_tests</commandlineArgs>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

这是失败的方式:

[INFO] Connecting to localhost/127.0.0.1:6300
[INFO] Connection refused (Connection refused)
[INFO] Connecting to localhost/127.0.0.1:6300
[INFO] Connection refused (Connection refused)
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  25.157 s
[INFO] Finished at: 2019-08-12T15:56:41-04:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.jacoco:jacoco-maven-plugin:0.8.2:dump (default-report-integration) on project: Unable to dump coverage data: Connection refused (Connection refused) -> [Help 1]

0 个答案:

没有答案