1.框架:
代码框架: spring boot
测试框架: junit-4(为spring-boot-test中自带版本(高版本如:2.3.4.RELEASE 是junit-5))
2.pom文件配置
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.4</version>
</dependency>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.4</version>
<configuration>
<!-- //统计的包-->
<includes>
<include>com/iwanvi/**</include>
</includes>
<!-- //排除的包-->
<excludes>
<!--排除的包-->
<exclude>com/xxx/comm/utils/**</exclude>
<exclude>com/xxx/common/annotation/**</exclude>
</excludes>
</configuration>
<executions>
<execution>
<id>pre-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>post-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
3.测试代码
@Test
public void demo() throws Exception {
System.out.println(port);
ResponseEntity<String> responseEntity = restTemplate.getForEntity("/iqiyi/pullMsg/book/list"
, String.class, "");
System.out.println(responseEntity.getBody());
MvcResult mvcResult = mockMvc.perform(
MockMvcRequestBuilders.get("/iqiyi/pullMsg/book/list")
.param("identity", "11111")
.param("lastUpdateTime", "22"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(MockMvcResultHandlers.print())
.andReturn();
System.out.println(mvcResult.getResponse().getContentAsString());
}
4.运行
运行后再target-site-jacoco中查看index.html
备注:pom中可配置覆盖率监控并设置阈值,在jenkins部署可一同使用,可自行某度查询