Spring Boot学习——单元测试
本随笔记录使用Spring Boot进行单元测试,主要是Service和API(Controller)进行单元测试。
一、Service单元测试
选择要测试的service类的方法,使用idea自动创建测试类,步骤如下。(注,我用的是idea自动创建,也可以自己手动创建)


自动创建测试类之后目录如下图:

测试类StudentServiceTest.java代码如下:
package *; //自己定义路径 import *.Student; //自己定义路径
import org.junit.Assert;
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.test.context.junit4.SpringRunner;
import static org.junit.Assert.*; @RunWith(SpringRunner.class)
@SpringBootTest
public class StudentServiceTest {
@Autowired
private StudentService studentService; @Test
public void findOne() throws Exception {
Student student = studentService.findOne(1);
Assert.assertEquals(new Integer(16), student.getAge());
}
}
二、API(Controller)单元测试
根据上面的步骤创建单元测试类StudentControllerTest.java,代码如下:
package *; //自己定义路径 import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import static org.junit.Assert.*; @RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class StudentControllerTest {
@Autowired
private MockMvc mvc; @Test
public void getStudentList() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/students"))
.andExpect(MockMvcResultMatchers.status().isOk());
//.andExpect(MockMvcResultMatchers.content().string("365")); //测试接口返回内容
} }
三、service和API(Controller)类和方法
StudentController.java代码如下:
package *; //自己定义路径 import *.StudentRepository; //自己定义路径
import *.Student; //自己定义路径
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List; @RestController
public class StudentController {
@Autowired
private StudentRepository studentRepository; /**
* 查询学生列表
* @return
*/
@GetMapping(value = "/students")
public List<Student> getStudentList(){
return studentRepository.findAll();
}
}
StudentService.java代码如下:
package *; //自己定义路径 import *.StudentRepository; //自己定义路径
import *.Student; //自己定义路径
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; @Service
public class StudentService {
@Autowired
private StudentRepository studentRepository; /**
* 根据ID查询学生
* @param id
* @return
*/
public Student findOne(Integer id){
return studentRepository.findOne( id);
}
}
Spring Boot学习——单元测试的更多相关文章
- Spring boot学习1 构建微服务:Spring boot 入门篇
Spring boot学习1 构建微服务:Spring boot 入门篇 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框 ...
- Spring Boot学习笔记2——基本使用之最佳实践[z]
前言 在上一篇文章Spring Boot 学习笔记1——初体验之3分钟启动你的Web应用已经对Spring Boot的基本体系与基本使用进行了学习,本文主要目的是更加进一步的来说明对于Spring B ...
- Spring Boot干货系列:(十二)Spring Boot使用单元测试(转)
前言这次来介绍下Spring Boot中对单元测试的整合使用,本篇会通过以下4点来介绍,基本满足日常需求 Service层单元测试 Controller层单元测试 新断言assertThat使用 单元 ...
- Spring Boot学习大全(入门)
Spring Boot学习(入门) 1.了解Spring boot Spring boot的官网(https://spring.io),我们需要的一些jar包,配置文件都可以在下载.添置书签后,我自己 ...
- Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客
==他的博客应该不错,没有细看 Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客 http://blog.csdn.net/u012706811/article/det ...
- spring boot 学习资料
spring boot 学习资料: 学习资料 网址 Spring Boot Cookbook-极客学院 http://wiki.jikexueyuan.com/project/spring-boot- ...
- spring boot 学习(十四)SpringBoot+Redis+SpringSession缓存之实战
SpringBoot + Redis +SpringSession 缓存之实战 前言 前几天,从师兄那儿了解到EhCache是进程内的缓存框架,虽然它已经提供了集群环境下的缓存同步策略,这种同步仍然需 ...
- 转载:spring boot学习
Spring Boot学习 Spring Boot是为了简化Spring应用的创建.运行.调试.部署等而出现的,使用它可以做到专注于Spring应用的开发,而无需过多关注XML的配置. 简单来说,它提 ...
- Spring Boot学习路线
Spring Boot 学习路线,本文计划根据作者近几年的工作.学习经验,来分析和制定一个学习使用 Spring Boot技术的步骤路线图. SpringBoot是伴随着Spring4.0诞生的: S ...
随机推荐
- BZOJ4972 小Q的方格纸
每个格子记录其左下的45°直角梯形区域的和及左下矩形区域的和即可. #include<iostream> #include<cstdio> #include<cmath& ...
- BZOJ3613 HEOI2014南园满地堆轻絮
不明白在某谷上是怎么标到紫的.二分答案或者发现答案就是最大逆序差的一半. #include<iostream> #include<cstdio> #include<cma ...
- 笔记-自己看Day20-待续
1. 搭建环境 1)注释掉csrf 2)配置模板路径 'DIRS': [os.path.join(BASE_DIR,'templates')], # BASE_DIR,代指当前目录. 3)配置静态文 ...
- 【刷题】清橙 A1295 necklace
试题来源 清华大学2011年百名信息学优秀高中学子夏令营 问题描述 有人打算送给你一条宝石项链,包含了N颗五颜六色(一共有M种颜色)的宝石.因为本问题中你只关心每个宝石的颜色,而且项链现在两头还没有接 ...
- 洛谷 P3157 [CQOI2011]动态逆序对 解题报告
P3157 [CQOI2011]动态逆序对 题目描述 对于序列\(A\),它的逆序对数定义为满足\(i<j\),且\(A_i>A_j\)的数对\((i,j)\)的个数.给\(1\)到\(n ...
- Lua弱表Weak table
定义:弱表的使用就是使用弱引用,很多程度上是对内存的控制. 1.weak表示一个表,它拥有metatable,并且metatable定义了__mode字段. 2.弱引用不会导致对象的引用计数变化.换言 ...
- swagger2的常用注解,传递参数的注意使用方法
背景介绍: 刚开始的时候,在controller层使用@RequestParam的时候,发现这个参数是必须要输入值的,但是我们有时候必须查询的时候允许参数为空,使用这个注解就不行了. 在集成了swag ...
- STL源码分析-iterator
http://note.youdao.com/noteshare?id=4efcb6441063dae956c226f91c161897
- STL源码分析-vector
http://note.youdao.com/noteshare?id=55dc651a7489f1413b3a3169401dcf94
- Qt ------ 主事件循环与 QEventLoop
1.事件循环一般用exec()函数开启.QApplicaion::exec().QMessageBox::exec()都是事件循环.其中前者又被称为主事件循环. 事件循环首先是一个无限“循环”,程序在 ...