前言
- Mockito 作用,部分代码段无法返回稳定的结果,这个时候就用 Mockito 拦截构造假数据返回。
- 比如:Dao 调用数据库,Http调用第三方接口。
步骤
- pom依赖。项目需要的所有依赖
- Main。Springboot 项目启动必须
- Controller 层。Http 请求提供
- Service 层。
- Dao 层。用于被拦截。
- 单元测试。
- 注意事项
1. pom依赖
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>Spring-mock</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>3.2.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.76</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2. Main
package com.jtfr;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootMain {
public static void main(String[] args) {
SpringApplication.run(SpringBootMain.class, args);
}
}
3. Controller 层
package com.jtfr.controller;
import com.jtfr.entity.User;
import com.jtfr.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/test")
public class TestController {
@Autowired
private TestService testService;
@PostMapping(value = "/postJson")
public String postJson(@RequestBody User user) {
return "TestController:" + testService.get(user);
}
}
4. Service 层
package com.jtfr.service;
import com.jtfr.dao.TestDao;
import com.jtfr.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class TestService {
@Autowired
private TestDao testDao;
public String get(User user) {
return "TestService:"+testDao.get(user);
}
}
5. Dao 层
package com.jtfr.dao;
import com.jtfr.entity.User;
import org.springframework.stereotype.Component;
@Component
public class TestDao {
public String get(User user) {
return "TestDao:"+user.toString();
}
}
6. 单元测试
package com.jtfr.controller;
import com.alibaba.fastjson.JSON;
import com.jtfr.SpringBootMain;
import com.jtfr.dao.TestDao;
import com.jtfr.entity.User;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener;
import org.springframework.http.MediaType;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import java.net.URI;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringBootMain.class)
@TestExecutionListeners(MockitoTestExecutionListener.class)
public class TestControllerTest extends AbstractTestNGSpringContextTests {
private MockMvc mockMvc;
@MockBean
private TestDao testDao;
@Autowired
private WebApplicationContext webApplicationContext;
@BeforeClass
public void before(){
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}
@Test
public void mockPost3() throws Exception {
Mockito.when(testDao.get(Mockito.any(User.class))).thenReturn("用的是mock数据");
User user = new User();
user.setId("1");
user.setUsername("xyz");
String userJsonStr = JSON.toJSONString(user);
ResultActions ra = mockMvc.perform(MockMvcRequestBuilders
.post(new URI("/test/postJson"))
.content(userJsonStr)
.contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)
);
MvcResult result = ra.andReturn();
System.out.println("result: " + result.getResponse().getContentAsString());
Thread.sleep(1000);
}
}
7. 注意事项
- Mockito 使用,必须要在 Test 类上使用 @TestExecutionListeners(MockitoTestExecutionListener.class)