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 ...
随机推荐
- 英文报道:China challenged Australian warships in South China Sea, reports say
学习地道新闻英语表达,以下文章来自CNN By Ben Westcott and Jamie Tarabay, CNN Updated 0830 GMT (1630 HKT) April 20, 20 ...
- 【BZOJ3105】新Nim游戏(线性基)
[BZOJ3105]新Nim游戏(线性基) 题面 BZOJ Description 传统的Nim游戏是这样的:有一些火柴堆,每堆都有若干根火柴(不同堆的火柴数量可以不同).两个游戏者轮流操作,每次可以 ...
- 【NOIP2017】列队(Splay)
[NOIP2017]列队(Splay) 题面 洛谷 题解 其实好简单啊... 对于每一行维护一棵\(Splay\) 对于最后一列维护一棵\(Splay\) \(Splay\)上一个节点表示一段区间 每 ...
- Linux内核设计第三周学习总结 跟踪分析Linux内核的启动过程
陈巧然 原创作品 转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 实验步骤 登陆实验楼虚 ...
- 2656: [Zjoi2012]数列(sequence)(递归+高精度)
好久没写题了T T NOIP 期中考双血崩 显然f(x)=f(x>>1)+f((x>>1)+1),考虑每次往x>>1递归,求出f(x),复杂度O(logN) 我们设 ...
- Caffe框架详细梳理
protobuf是google公司开发的,并在Google内部久经考验的一个东西,在08年google把它贡献给了开源社区,随后便有越来越多的人使用它.protobuf是一个结构化信息传递的工具,主要 ...
- 使图片水平并垂直居中的一个Hack
淘宝的一个前端面试题:使用纯CSS实现未知尺寸的图片(但高宽都小于200px)在200px的正方形容器中水平和垂直居中. 想起了vertical-align:middle;但是不行,后来才知道还要di ...
- TCP/IP地址格式转换API
1.htonl ()和ntohl( ) ntohl( )-----网络顺序转换成主机顺序(长整型) u_long PASCAL FAR ntohl (u_long netlong); htonl ( ...
- Rabbitmq--topic
一.前言 前面讲到direct类型的Exchange路由规则是完全匹配binding key与routing key,但这种严格的匹配方式在很多情况下不能满足实际业务需求.topic类型的Exchan ...
- 怎样安装Command Line Tools in OS x Mavericks&Yosemite(Without xcode)--转载
How to Install Command Line Tools in OS X Mavericks & Yosemite (Without Xcode) Mac users who pre ...