什么是mock?
mock指的是通过模拟数据来使程序完成流程的运行,简单说一句就是自己造数据模拟流程。
mock有哪些工具?
JUnit
参考:https://junit.org/junit5/
TestNG
参考:https://testng.org/doc/
AssertJ
参考:https://joel-costigliola.github.io/assertj/
MockObjects.com
参考:http://www.mockobjects.com/
Mockito
参考:https://site.mockito.org/
EasyMock
参考:https://easymock.org/
JMock
参考:https://jmock.org/
DbUnit
参考:https://www.dbunit.org/
Testcontainers
参考:https://www.testcontainers.org/
Grinder
参考:https://sourceforge.net/projects/grinder/
SpringMockK
參考:https://github.com/Ninja-Squad/springmockk
spring自带(spring-test):
参考:
https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/testing.html
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/mock/web/package-summary.html
https://blog.csdn.net/u013553309/article/details/89680632?biz_id=102&utm_term=spring%20mock&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-0-89680632&spm=1018.2118.3001.4187
当然还有很多工具....你确定你学得完?还是找几个常用的用吧,毕竟这些只是辅助,大部分都大同小异,可以自行了解需要的。建议用spring自带+junit最多再加一个testNG
以下是关于spring测试的相关,详细可以查看:
https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/testing.html#testing-introduction
代码下载:https://gitee.com/hong99/spring/issues/I1N1DF
实现源码
注:在项目base_spring 下面新增包
项目结构
ReflectionTestUtils
为spring自带的一个mock工具,当然只是冰山一角,需要更详细了解可以上官网。
com.hong.spring.junit.bean.Student
package com.hong.spring.junit.bean;
/**
*
* 功能描述:学生
*
* @param:
* @return:
* @auther: csh
* @date: 2020/7/22 18:53
*/
public class Student {
private Integer age;
private String name;
private Integer id;
public void setAge(Integer age) {
this.age = age;
}
public Integer getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
@Override
public String toString() {
return "Student{" +
"age=" + age +
", name='" + name + '\'' +
", id=" + id +
'}';
}
}
com.hong.spring.junit.UserService
package com.hong.spring.junit;
import com.hong.spring.junit.bean.Student;
import org.springframework.beans.factory.annotation.Autowired;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
/**
* @Auther: csh
* @Date: 2020/10/12 17:17
* @Description:用户服务
*/
public class UserService{
@Autowired
private String userName;
@Autowired
private Student student;
@PostConstruct
private void start(){
System.out.println("start:"+userName);
System.out.println("start:"+student);
}
@PreDestroy
private void stop(){
System.out.println("stop:"+userName);
System.out.println("stop:"+student);
}
}
MockMvc(调用控制器层)
MockMvc为spring自带mock工具可以通过该功能模拟相关的请求api;
junit.UserServiceTest
package junit;
import com.hong.spring.junit.UserService;
import com.hong.spring.junit.bean.Student;
import org.junit.Test;
import org.springframework.test.util.ReflectionTestUtils;
/**
* @Auther: csh
* @Date: 2020/10/12 17:21
* @Description:测试:使用ReflectionTestUtils解决依赖注入
*/
public class UserServiceTest {
/**
*
* 功能描述:通过ReflectionTestUtils测试
*
* @param:
* @return:
* @auther: csh
* @date: 2020/10/12 17:23
*/
@Test
public void testUserService(){
UserService userService = new UserService();
ReflectionTestUtils.setField(userService,"userName","hong");
Student student = new Student();
student.setAge(100);
student.setId(1);
student.setName("hong99");
ReflectionTestUtils.setField(userService,"student",student);
ReflectionTestUtils.invokeMethod(userService,"start");
ReflectionTestUtils.invokeMethod(userService,"stop");
}
}
运行结果
start:hong
start:Student{age=100, name='hong99', id=1}
stop:hong
stop:Student{age=100, name='hong99', id=1}
junit.MockControllerTest
注:关于控制器是原已有建议下载原码学习!
/**
* @Auther: csh
* @Date: 2020/10/13 10:14
* @Description:通过mock请求控制器
*/
public class MockControllerTest {
@Test
public void testHomePage() throws Exception {
HomeController controller = new HomeController();
MockMvc mockMvc = standaloneSetup(controller).build();
mockMvc.perform(get("/"))
.andExpect(view().name("home"));
}
}
运行结果
2020-10-13 10:16:32,577 [main] INFO : 进入了首页!
2020-10-13 10:16:32,582 [main] ERROR: error进入了首页!
代码下载:https://gitee.com/hong99/spring/issues/I1N1DF
最后
在真实工作中,与第三方联调是家常便饭,但是有时候真的人家的接口会晚几天才提供过来,总不能一直等着,通过mock自己模拟一些数据进行测试先把自己的流程跑通,后面再统一联调,而spring提供的mock也很多,ReflectionTestUtils只是其中的一种,可以看看官方文档统一尝试,是一种提升工作效率和质量不错的工具,推荐。
参考文章:
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/test/util/ReflectionTestUtils.html
https://www.lagou.com/lgeduarticle/125128.html
https://www.baeldung.com/spring-reflection-test-utils
https://www.tianmaying.com/tutorial/JunitForSpringBoot