如何为在 Spring boot 中开发的其余端点获取 Rest Assured 测试用例的代码覆盖率?

问题描述 投票:0回答:0

我正在尝试为指向其余端点的放心测试用例生成 jacoco 代码覆盖率报告。我正在使用 springboot 应用程序。 我的控制器类看起来像:

@RestController
public class ChallengeController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ChallengeController.class);
    @Autowired
    private ChallengeService challengeService;


    public ChallengeController(ChallengeService challengeService) {
        this.challengeService = challengeService;
    }

    


    @GetMapping(path = "/hello",
            produces = MediaType.TEXT_PLAIN_VALUE)
    @ResponseBody
    public String hello() {
        return "hello";
    }
    @GetMapping(path = "/hello/greeting/{name}",
            produces = MediaType.TEXT_PLAIN_VALUE)
    @ResponseBody
    public String greeting(@PathVariable(value = "name") String name) {
        return challengeService.greeting(name);
    }

}

我的服务类看起来像

@Service
@Component
public class ChallengeService {

    
    private static final Logger LOGGER = LoggerFactory.getLogger(ChallengeService.class);



    public ChallengeService() {

    }


    public String greeting(String name) {
        return "hello " + name;
    }
}

我的 POM 看起来像

  <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${maven-surefire-plugin.version}</version>
                <configuration>
                    <argLine>${surefireArgLine}</argLine>
                    <excludes>
                        <exclude>**/ResilienceTests.java</exclude>
                        <exclude>**/CucumberTest.java</exclude>
                        <exclude>**/DummyChallengeTest.java</exclude>
                    </excludes>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>${jacoco-maven-plugin.version}</version>
                <executions>
                    <execution>
                        <id>pre-unit-test</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                        <configuration>
                            <destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile>
                            <propertyName>surefireArgLine</propertyName>
                        </configuration>
                    </execution>
                    <execution>
                        <id>post-unit-test</id>
                        <phase>test</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                        <configuration>
                            <dataFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</dataFile>
                            <outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
                        </configuration>
                    </execution>
                    <execution>
                        <id>pre-integration-test</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>prepare-agent-integration</goal>
                        </goals>
                        <configuration>
                            <destFile>${project.build.directory}/coverage-reports/jacoco-it.exec</destFile>
                            <propertyName>failsafeArgLine</propertyName>
                        </configuration>
                    </execution>
                    <execution>
                        <id>post-integration-test</id>
                        <phase>post-integration-test</phase>
                        <goals>
                            <goal>report-integration</goal>
                        </goals>
                        <configuration>
                        <!-- excluding everything except controller class -->
                            <excludes>
                                <exclude>**/common/*.class</exclude>
                                <exclude>**/config/*.class</exclude>
                                <exclude>**/domain/*.class</exclude>
                                <exclude>**/exception/*.class</exclude>
                                <exclude>**/mapper/*.class</exclude>
                                <exclude>**/service/*.class</exclude>
                                <exclude>**/simulator/Main.class</exclude>
                            </excludes>
                            <dataFile>${project.build.directory}/coverage-reports/jacoco-it.exec</dataFile>
                            <outputDirectory>${project.reporting.outputDirectory}/jacoco-it</outputDirectory>
                            <sourceEncoding>${project.build.sourceEncoding}</sourceEncoding>
                            <classFilesDIrectory>${project.build.directory}/classes</classFilesDIrectory>
                        </configuration>
                    </execution>
                    <!-- merging jacoco reports-->
                    <execution>
                        <id>report-aggregate</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>report-aggregate</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>merge-results</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>merge</goal>
                        </goals>
                        <configuration>
                            <fileSets>
                                <fileSet>
                                    <directory>${project.build.directory}/coverage-reports/</directory>
                                    <includes>
                                        <include>*.exec</include>
                                    </includes>
                                </fileSet>
                            </fileSets>
                            <destFile>${project.build.directory}/coverage-reports/aggregate.exec</destFile>
                        </configuration>
                    </execution>
                    <execution>
                        <id>post-merge-report</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                        <configuration>
                            <dataFile>${project.build.directory}/coverage-reports/aggregate.exec</dataFile>
                            <outputDirectory>${project.reporting.outputDirectory}/jacoco-aggregate</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

     <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>${maven-failsafe-plugin.version}</version>
                <executions>
                    <execution>
                        <id>integration-tests</id>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                        <configuration>
                            <argLine>${failsafeArgLine}</argLine>
                            <includes>
                                <include>**/*CucumberTest.java</include>
                                <include>**/*DummyChallengeTest.java</include>
                            </includes>
                            <forkCount>1</forkCount>
                            <reuseForks>true</reuseForks>
                            <!-- When running as a Maven plugin, the JaCoCo agent configuration is prepared by invoking the prepare-agent
                            or prepare-agent-integration goals, before the actual tests are run. This sets a property named argLine which
                            points to the JaCoCo agent, later passed as a JVM argument to the test runner -->

                        </configuration>
                    </execution>
                </executions>
            </plugin>

我的测试类看起来像


@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)

public class DummyChallengeTest extends AbstractSteps {



    @Test
    public void testHelloEndpoint() {        
        RestAssured.baseURI = "http://localhost:8082";
        RestAssured.basePath ="/hello";
        Response res = given()
                .when().get()
                .then().log().all()
                .extract().response();
        Assert.assertEquals("hello", res.getBody().asString());
    }

    @Test
    public void testGreetingEndpoint() {
        String uuid = UUID.randomUUID().toString();
        //getServiceUrl() returning me the application base url
        String requestUrl = getServiceUrl() + "/hello/greeting/{name}";
                given()
                .pathParam("name", uuid)
                .when().get(requestUrl)
                .then().log().all()
                .statusCode(200)
                .body(is("hello " + uuid)).log().all().extract();
    }
}

在 target/site/jacoco-it/jacoco-sessions.html 里面,我可以找到我的控制器和服务 java 文件。

我只在这些行中部分覆盖了我的控制器文件


   @Autowired
    private ChallengeService challengeService;


    public ChallengeController(ChallengeService challengeService) {
        this.challengeService = challengeService;
    }

但是我没有得到关于终点的任何报道,特别是在这些线上

@GetMapping(path = "/hello",
            produces = MediaType.TEXT_PLAIN_VALUE)
    @ResponseBody
    public String hello() {
        return "hello";
    }
    @GetMapping(path = "/hello/greeting/{name}",
            produces = MediaType.TEXT_PLAIN_VALUE)
    @ResponseBody
    public String greeting(@PathVariable(value = "name") String name) {
        return challengeService.greeting(name);
    }

注意:1.我放心的测试用例运行良好并且全部通过。 2. 当我使用 Quarkus 应用程序和 Quarkus 测试时,同样的 jacoco 实现让我完全覆盖端点。 3. 我正在全面覆盖我的单元测试用例。

当我使用 Quarkus 应用程序和 Quarkus 测试时,同样的 jacoco 实现让我完全覆盖端点。

我看到有人说要从同一个 jvm 运行你的应用程序和测试。我不确定如何正确执行此操作。此外,如果可以在 jenkins 管道中实施此解决方案。

我还没有尝试过 Arquillian。

java rest integration-testing code-coverage jacoco-maven-plugin
© www.soinside.com 2019 - 2024. All rights reserved.