1.什么是Mockito?
Mockito是一个模拟测试框架,可以让你用优雅,简洁的接口写出漂亮的单元测试。Mockito可以让单元测试易于可读,产生简洁的校验错误。
使用场景
- 提前创建测试,TDD(测试驱动开发)
- 团队可以并行工作
- 你可以创建一个验证或者演示程序
- 为无法访问的资源编写测试
- Mock可以交给用户
- 隔离系统
2.代码工程
实验目的
mock controller and service
pom.xml
spring-boot-starter-test 中包含 junit5 和 Mockito 相关jar。无需额外引入。
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>springboot-demo</artifactId><groupId>com.et</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>Mockito</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-autoconfigure</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><scope>test</scope></dependency></dependencies>
</project>
controller
package com.et.mockito.controller;import com.et.mockito.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.HashMap;
import java.util.Map;@RestController
public class HelloWorldController {@AutowiredHelloService helloService;@RequestMapping("/hello")public Map<String, Object> showHelloWorld(){Map<String, Object> map = new HashMap<>();map.put("msg", helloService.sayhi("hblog"));return map;}
}
service
package com.et.mockito.service;public interface HelloService {public String sayhi(String name);
}
package com.et.mockito.service;import org.springframework.stereotype.Service;/*** @author liuhaihua* @version 1.0* @ClassName HelloServiceImpl* @Description todo* @date 2024/09/09/ 17:44*/
@Service
public class HelloServiceImpl implements HelloService{@Overridepublic String sayhi(String name) {return "hi"+name;}
}
以上只是一些关键代码,所有代码请参见下面代码仓库
代码仓库
- https://github.com/Harries/springboot-demo(Mockito )
3.测试
mock controller
@Test
public void mockController() throws Exception {// mock servicewhen(helloService.sayhi("hblog")).thenReturn("harries");//mock controllerResultActions perform = this.mockMvc.perform(get("/hello"));log.info(perform.andReturn().getResponse().getContentAsString());//verify responsibleperform.andExpect(status().isOk()).andExpect(MockMvcResultMatchers.content().json("{msg:harries}")); // You don't need to write it in escaped json format
}
mock Service
@Test
public void mock1() {//Mockito.when(helloService.sayhi(Mockito.anyString())).thenReturn("harries");when(helloService.sayhi("hblog")).thenReturn("harries");String str = helloService.sayhi("hblog");log.info(str);Assert.assertNotNull(str);}
@Test
public void mock2() {when(helloService.sayhi("exception")).thenThrow(new RuntimeException("mock throw exception"));String str1 = helloService.sayhi("exception");}@Test
public void mock3() {helloService.sayhi("hblog");Mockito.verify(helloService, Mockito.times(1)).sayhi(Mockito.eq("hblog")) ;}