Jacoco 不更新 MockMvc 测试的完整性

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

我有一个带有一些 RestController 的 Spring Boot 应用程序。

我正在使用 MockMvc 测试 RestController,以重现与最终用户相同的行为。

问题是 Jacoco 没有更新这些测试的完整性覆盖率:

Jacoco报告:

我的睾丸是为LegacyController制作的

LegacyController 有一个简单的结构,像这样(我需要修改,因为我公司的政策):

  @GetMapping("/{thing}/{thing2}")
    @ResponseStatus(value = HttpStatus.CREATED)
    public ResponseEntity<Object> doSomeThing(@PathVariable("thing") String thing,
                                                     @PathVariable("service") String thing2,
                                                     @RequestBody(required = false) String boody,
                                                     @RequestHeader("Header1") String Header1,
                                                     @RequestHeader("Content-Type") String contentType,
                                                     @RequestParam(value = "OtherThing") String OtherThing,
                                                     @RequestParam(value = "OtherThing2", defaultValue = "defaultValue", required = false) String OtherThing2) {

        return legacyService.crud(thing, thing2, request, Header1, OtherThing, outputType, OtherThing2);
    }

我的测试做了这样的事情:

@RunWith(SpringRunner.class)
@Import(LegacyController.class)
@SpringBootTest
@AutoConfigureMockMvc
@WebAppConfiguration
@ActiveProfiles("test")
public class LegacyControllerTest {

    private MockMvc mockMvc;

    @MockBean
    private LegacyService legacyService;


    @Autowired
    private WebApplicationContext context;

    @Before
    public void setUp(){
        this.mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
    }

    @Test
    public void givenGetJson_WhenOffsetPageIsPresent_ThenReturnsOk() throws Exception {
        Mockito.doReturn("OK").when(legacyService).request(Mockito.any(), Mockito.anyString(), Mockito.any(TokenDTO.class), Mockito.any(HttpHeaders.class));
       

        mockMvc.perform(MockMvcRequestBuilders
                .get("/gateway/v1/{thing}/{thing2}", "value-thing", "value-thing2")

                .header("Content-Type", "application/json")

                .header("Header1", "Header1 value")
                .queryParam("OtherThing", "OtherThing value")
                .queryParam("OtherThing2", "OtherThing2 Value")
                .content("A BODY GOES HERE"))

                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk());

    }
    }

这是pom.xml


<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.2</version>
        <relativePath/>
        <!-- lookup parent from repository -->
    </parent>


 ***HIDDEN DATA****

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>

*** MORE HIDDEN DATA ***



    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M7</version>
            </plugin>

            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.8</version>
                <configuration>
                    <excludes>
                        <exclude>**/constant/*</exclude>
                        <exclude>**/dto/*</exclude>
                        <exclude>**/enuns/*</exclude>
                        <exclude>**/exception/*</exclude>
                        <exclude>**/ApiApplication.class</exclude>
                    </excludes>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>generate-code-coverage-report</id>
                        <phase>test</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>


        </plugins>
    </build>
</project>

我的测试工作正常,做它的设计目的。

问题是代码覆盖率没有改变。

java spring-boot code-coverage jacoco mockmvc
1个回答
0
投票

我遇到了同样的问题,我通过将

org.junit.Test
替换为
org.junit.jupiter.api.Test

解决了

但是,我不知道为什么它有效。

© www.soinside.com 2019 - 2024. All rights reserved.