SpringBoot使用MockMVC进行Junit测试并使用Jacoco查看测试的代码覆盖率

一、MockMVC简介
MockMVC实现了对HTTP请求的模拟(并不是真正的HTTP请求),能够直接调用Controller进行测试,测试速度快、不依赖网络环境

二、使用案例
案例的代码链接附在文章最后

1)、被测试的Controller

@RestController
@RequestMapping("/api/product")
public class ProductController {
    @Autowired
    private ProductService productService;

    @GetMapping("/findAllProduct")
    public List<Product> findAllProduct() {
        return productService.findAllProduct();
    }

    @GetMapping("/getProductById")
    public Product getProductById(Integer id) {
        return productService.getProductById(id);
    }

    @DeleteMapping("/deleteProductById")
    public boolean deleteProductById(Integer id) {
        productService.deleteProductById(id);
        return true;
    }

    @PostMapping("/insertProduct")
    public boolean insertProduct(@RequestBody Product product) {
        productService.insertProduct(product);
        return true;
    }

    @PostMapping("/updateProduct")
    public boolean updateProduct(@RequestBody Product product) {
        productService.updateProduct(product);
        return true;
    }
}

2)、测试类(主要测试的是findAllProduct和insertProduct)

package com.hand.druidTest;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

//事务回滚(测试的时候不会产生脏数据)
@Rollback
@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional
public class DruidTestApplicationTests {

    private MockMvc mockMvc;

    @Autowired
    protected WebApplicationContext wac;

    @Before  //这个方法在每个方法执行之前都会执行一遍
    public void init() {
        mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();  //初始化MockMvc对象
    }

    @Test
    public void findAllProductTest() throws Exception {
        String responseString = mockMvc.perform(
                get("/api/product/findAllProduct")    //请求的url,请求的方法是get
                //数据的格式
        ).andExpect(status().isOk())    //返回的状态是200
                .andDo(print())         //打印出请求和相应的内容
                .andReturn().getResponse().getContentAsString();   //将相应的数据转换为字符串
        System.out.println("返回的json1= " + responseString);
    }

    @Test
    public void insertProductTest() throws Exception {
        String example = "{\"name\":\"移动电话\",\"price\":1999}";
        String responseString = mockMvc.perform(
                post("/api/product/insertProduct")
                        //数据的格式
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(example)).
                andExpect(status().isOk())
                .andDo(print())
                .andReturn().getResponse().getContentAsString();
        System.out.println("返回的json2= " + responseString);
    }
}

三、JaCoCo简介
JaCoCo可以监控测试过程中的代码覆盖率,IDEA提供了JaCoCo的插件

四、JaCoCo的使用
1)、点击右上角修改相关配置
在这里插入图片描述

2)、点击Junit的启动类,在Code Coverage中选择JaCoCo(如果左侧没有Junit启动类请先运行Junit的启动类)
在这里插入图片描述

3)、点击Junit启动类,选择Run ‘xxx’ with Coverage

在这里插入图片描述

4)、运行结束后,右上角弹出测试信息,点击下图中按钮选择测试报告生成的路径
在这里插入图片描述

5)、进入生成报告的目录下,打开index.html

在这里插入图片描述

每个Element点进去可以看到具体的方法的覆盖情况

案例链接:https://pan.baidu.com/s/1Rac5WllGSc1fTdnzGD75Nw
(里面有SQL文件,需要在application.properties里面修改一下MySQL的配置信息) 提取码:1r24

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值