spring boot junit test
这里分三种,1、测普通方法或通过原生java API接口调用 2、基于spring依赖注入调用 3、controller层调用
需要引入依赖:默认springboot已经引入
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
在src/test/java下建立test类
1、测普通方法或通过原生java API接口调用
public class commonTest {
@Test
public void testPrint() {
System.out.println(2222222);
}
}
2、基于spring依赖注入调用 内部可以通过@autowired、@Resourced等注入对象实例
@RunWith(SpringRunner.class)
@SpringBootTest
public class applicationTest {
//注入的接口类
@Autowired
private TestService testService;
@Test
public void contextLoads() throws Exception{
testService.print();
}
}
public interface TestService {
public void print()throws Exception;
}
@Service("testService")
public class TestServiceImpl implements TestService {
@Override
public void print() throws Exception {
// TODO Auto-generated method stub
System.out.println("service print test...");
}
}
3、 controller层调用
@SpringBootTest
public class ControllerTest {
private MockMvc mockMvc;
//@Before注解的表示在测试启动的时候优先执行,一般用作资源初始化。
//这里初始化生成controller类单例
@Before
public void setUp()throws Exception{
mockMvc=MockMvcBuilders.standaloneSetup(new TestController()).build();
}
@Test
public void controllerTest()throws Exception{
String returnJson = mockMvc.perform(MockMvcRequestBuilders.post("/list")).andReturn().getResponse().getContentAsString();
System.out.println(returnJson);
}
}
@RestController
public class TestController {
@RequestMapping(value="/list",method={RequestMethod.POST})
public List<TestVO> getTestList() {
List<TestVO> vos = new ArrayList<TestVO>();
TestVO vo = new TestVO();
vo.setAge(13);
vo.setName("薛邵");
vo.setSex(true);
vo.setDate(new Date());
vos.add(vo);
TestVO vo1 = new TestVO();
vo1.setAge(15);
vo1.setName("xiaoming");
vo1.setSex(false);
vo1.setDate(new Date());
vos.add(vo1);
return vos;
}
}
MockMvc 调用controller接口的几个示例:
A
//get请求一个查询/test/hhhhhhh/99,控制台打印http请求和响应信息
//print()方法,需要静态引入import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
mockMvc.perform(MockMvcRequestBuilders.get("/test/hhhhhhh/99").accept(MediaType.APPLICATION_JSON_UTF8)).andDo(print());
打印示例如下:
其中Body= aaaaaaaaaaaaaaaaaaaaaaaahhhhhhh99即我们预期打印的内容,也就是controller接口返回的文本
B
//通过.addExpect来判断预期内容是否符合,如果符合控制台无信息,如果不符合,junit控制台会显示具体错误信息
//.accept(MediaType.APPLICATION_JSON_UTF8)
//这句主要是设置JSON返回编码,避免出现中文乱码问题
mockMvc.perform(MockMvcRequestBuilders.get("/test/hhhhhhh/99").accept(MediaType.APPLICATION_JSON_UTF8)).andExpect(MockMvcResultMatchers.content().string(Matchers.containsString("hhhhhhh991")));
C
//获取返回内容直接输出打印
String returnJson = mockMvc.perform(MockMvcRequestBuilders.post("/testvo")).andReturn().getResponse().getContentAsString();
System.out.println(returnJson);
D
//设置参数POST提交
mockMvc.perform(MockMvcRequestBuilders.post("/v")
// .param("age", "28")
// .param("name", "aaa")
// .param("list", "[\"bb\",\"cc\"]")
// .param("card", "123456789012345678")
// .param("date", "2019-10-01 11:09:11")
// .param("weight", "99.99")
// .param("sex", "true")
//// .param("tmp", "")
//// .param("phone", "")
// .param("dicimal", "18")
// .param("email", "aaa")
);
spring boot junit test的更多相关文章
- 学习 Spring Boot:(二十九)Spring Boot Junit 单元测试
前言 JUnit 是一个回归测试框架,被开发者用于实施对应用程序的单元测试,加快程序编制速度,同时提高编码的质量. JUnit 测试框架具有以下重要特性: 测试工具 测试套件 测试运行器 测试分类 了 ...
- Spring boot Junit Test单元测试
Spring boot 1.40 JUnit 4 需要依赖包 spring-boot-starter-test.spring-test 建立class,加上如下注解,即可进行单元测试,别的帖子里说要加 ...
- (27)Spring Boot Junit单元测试【从零开始学Spring Boot】
Junit这种老技术,现在又拿出来说,不为别的,某种程度上来说,更是为了要说明它在项目中的重要性. 那么先简单说一下为什么要写测试用例 1. 可以避免测试点的遗漏,为了更好的进行测试,可以提高测试效率 ...
- (转)Spring Boot Junit单元测试
场景:在项目开发中要测试springboot工程中一个几个dao和service的功能是否正常,初期是在web工程中进行要素的录入,工作量太大.使用该单元测试大大减小了工作强度. Junit这种老技术 ...
- spring boot junit controller
MockMvc 来自Spring Test,它允许您通过一组方便的builder类向 DispatcherServlet 发送HTTP请求,并对结果作出断言.请注意,@AutoConfigureMoc ...
- spring boot -junit单元测试方法示例
package com.example.zs; import com.example.zs.mapper.UserMapper; import com.example.zs.pojo.User; im ...
- Spring Boot Junit单元测试
http://blog.csdn.net/catoop/article/details/50752964
- 83. Spring Boot 1.4单元测试【从零开始学Spring Boot】
在[27. Spring Boot Junit单元测试]中讲过1.3版本的单元测试方式,这里说说1.4和1.3有什么区别之处? 在1.3中单元测试这样子的类似代码: //// SpringJUnit支 ...
- Spring Boot(十二)单元测试JUnit
一.介绍 JUnit是一款优秀的开源Java单元测试框架,也是目前使用率最高最流行的测试框架,开发工具Eclipse和IDEA对JUnit都有很好的支持,JUnit主要用于白盒测试和回归测试. 白盒测 ...
随机推荐
- Django--实现分页功能,并且基于cookie实现用户定制每页的数据条数
# page_num 当前页数, total_result_num 总共有多少条测试结果 def pagination(request, page_num, total_result_num, res ...
- O(n)时间复杂度查找数组第二大元素
分析:要求O(n)时间复杂度,不能用排序.可以设置两个临时变量分别保存当前最大值以及当前第二大的值,然后遍历数组,不断更新最大值和第二大的数值. 代码: bool findSec(vector< ...
- CSS template
ylbtech-CSS3: 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 作者:ylbtech出处:http://ylbtech.cn ...
- java url中文参数乱码
String city=new String(city_name.getBytes("ISO-8859-1"), "UTF-8");
- 20140806 交换两个数 extern “C”用法
1.交换两个数 方法1.a+b有可能越界 a=a+b; b=a-b; a=a-b; 方法二.不会越界 a=a^b b=a^b; a=a^b; 2.extern "C"用法 ( ...
- ajax 回传参数
JSONObject json = new JSONObject(); json.put("msg", msg); json.put("success", co ...
- STL 队列模板实现
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u010016150/article/details/32715801 C++ Prime确实有点 ...
- mongodb入门篇
MongoDB 入门篇 分类: NoSQL, 故障解决 undefined 1.1 数据库管理系统 在了解MongoDB之前需要先了解先数据库管理系统 1.1.1 什么是数据? 数据(英语:data) ...
- PHP72w安装
PHP72w # rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm # rpm ...
- java反射机制以及应用
JAVA反射机制+动态运行编译期不存在的JAVA程序 一.有关JAVA反射 在运行期间,在不知道某个类A的内部方法和属性时,能够动态的获取信息.获取类或对象的方法.属性的功能,称之为反射. 1.相关类 ...