You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JUnit测试运行全绿,基于Wiremock的测试对应Controller代码覆盖率为0

问题描述

测试用例运行全部通过,但IntelliJ统计的Dummy Controller/Resource类代码覆盖率为0,相关代码如下:

BaseControllerTest父类代码

private static int port;
public static String hostUrl;
protected static WireMockServer wireMockServer;
protected static final ObjectMapper mapper = new ObjectMapper();

public static RestTemplate restTemplate;
public static final String HOST = "localhost";  


@Rule
    public ExpectedException thrown = ExpectedException.none();    
@BeforeClass
    public static void init() {
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        wireMockServer = new WireMockServer(WireMockConfiguration.wireMockConfig().dynamicPort());
        wireMockServer.start();
        port = wireMockServer.port();
        WireMock.configureFor(HOST, port);
        hostUrl = "http://" + HOST + ":" + port;
        restTemplate = new RestTemplate();
    }

    @AfterClass
    public static void destroy() {
        wireMockServer.stop();
    }

DummyResourceTest继承BaseControllerTest的测试用例代码

@Test
public void testScenario1WithSuccess() throws JAXBException {
    stubFor(post(urlPathEqualTo(END_POINT))
            .withHeader(HttpHeaders.ACCEPT, equalTo(MediaType.APPLICATION_JSON))
            .withHeader(HttpHeaders.CONTENT_TYPE, equalTo(MediaType.APPLICATION_JSON))
            .withHeader(HttpHeaders.AUTHORIZATION, equalTo(TEST_SERVICE_TOKEN))
            .withRequestBody(containing(TEST_FIELD_1))
            .willReturn(aResponse().withStatus(200)
                    .withHeader(CONTENT_TYPE, MediaType.APPLICATION_JSON)
                    .withBody(readJsonFile("Sample_Response.json"))));

    RequestObject request = new RequestObject();
    request.setField1("111");

    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.setContentType(org.springframework.http.MediaType.APPLICATION_JSON);
    httpHeaders.add(ACCEPT, APPLICATION_JSON);
    httpHeaders.set(AppConstants.AUTHORIZATION,  TEST_SERVICE_TOKEN);
    HttpEntity<Object> requestEntity = new HttpEntity<>(request, httpHeaders);


    ResponseEntity<String> responseEntity =  restTemplate.exchange(END_POINT,
            HttpMethod.POST, requestEntity,
            String.class);

    assertNotNull(responseEntity);
    assertEquals(HttpStatus.OK.value(),responseEntity.getStatusCodeValue());
    
}
问题原因与解决方案
  • 核心原因:你当前的测试逻辑完全没有调用到本地的Dummy Controller/Resource业务代码。你用WireMock启动了模拟服务器,直接把要测试的END_POINT接口的响应Mock掉了,RestTemplate发出的请求全部打到了WireMock的模拟服务器上,并没有请求你自己的Spring服务实例,你的Controller代码全程没有执行,所以覆盖率统计为0。
  • 修正步骤:
    1. 补充Spring测试注解加载业务上下文:给测试类添加@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)注解,启动真实的Spring Web服务,把你的Dummy Controller加载到服务容器中。
    2. 调整WireMock的使用场景:WireMock应该用来Mock你的Controller内部依赖的第三方外部接口,而非你要测试的Controller本身的接口。
    3. 修改请求地址:将RestTemplate的请求地址改为你本地启动的Spring服务的地址+端口,可以通过@LocalServerPort注解注入Spring服务启动的随机端口,拼接成正确的请求地址发起测试请求。
    4. 检查覆盖率统计配置:确认IntelliJ的覆盖率统计范围包含了你的Dummy Controller所在的包路径,没有被排除在统计范围外。

内容的提问来源于stack exchange,提问作者user1398291

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 17:27:03