一、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