本随笔记录使用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学习——单元测试的更多相关文章

  1. Spring boot学习1 构建微服务:Spring boot 入门篇

    Spring boot学习1 构建微服务:Spring boot 入门篇 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框 ...

  2. Spring Boot学习笔记2——基本使用之最佳实践[z]

    前言 在上一篇文章Spring Boot 学习笔记1——初体验之3分钟启动你的Web应用已经对Spring Boot的基本体系与基本使用进行了学习,本文主要目的是更加进一步的来说明对于Spring B ...

  3. Spring Boot干货系列:(十二)Spring Boot使用单元测试(转)

    前言这次来介绍下Spring Boot中对单元测试的整合使用,本篇会通过以下4点来介绍,基本满足日常需求 Service层单元测试 Controller层单元测试 新断言assertThat使用 单元 ...

  4. Spring Boot学习大全(入门)

    Spring Boot学习(入门) 1.了解Spring boot Spring boot的官网(https://spring.io),我们需要的一些jar包,配置文件都可以在下载.添置书签后,我自己 ...

  5. Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客

    ==他的博客应该不错,没有细看 Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客 http://blog.csdn.net/u012706811/article/det ...

  6. spring boot 学习资料

    spring boot 学习资料: 学习资料 网址 Spring Boot Cookbook-极客学院 http://wiki.jikexueyuan.com/project/spring-boot- ...

  7. spring boot 学习(十四)SpringBoot+Redis+SpringSession缓存之实战

    SpringBoot + Redis +SpringSession 缓存之实战 前言 前几天,从师兄那儿了解到EhCache是进程内的缓存框架,虽然它已经提供了集群环境下的缓存同步策略,这种同步仍然需 ...

  8. 转载:spring boot学习

    Spring Boot学习 Spring Boot是为了简化Spring应用的创建.运行.调试.部署等而出现的,使用它可以做到专注于Spring应用的开发,而无需过多关注XML的配置. 简单来说,它提 ...

  9. Spring Boot学习路线

    Spring Boot 学习路线,本文计划根据作者近几年的工作.学习经验,来分析和制定一个学习使用 Spring Boot技术的步骤路线图. SpringBoot是伴随着Spring4.0诞生的: S ...

随机推荐

  1. 【Mybatis】简单的mybatis增删改查模板

    简单的mybatis增删改查模板: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE map ...

  2. 深入理解JVM一垃圾回收器

    上一篇我们介绍了常见的垃圾回收算法,不同的算法各有各的优缺点,在JVM中并不是单纯的使用某一种算法进行垃圾回收,而是将不同的垃圾回收算法包装在不同的垃圾回收器当中,用户可以根据自身的需求,使用不同的垃 ...

  3. 洛谷P4559 [JSOI2018]列队 【70分二分 + 主席树】

    题目链接 洛谷P4559 题解 只会做\(70\)分的\(O(nlog^2n)\) 如果本来就在区间内的人是不用动的,区间右边的人往区间最右的那些空位跑,区间左边的人往区间最左的那些空位跑 找到这些空 ...

  4. Redis Scan迭代器遍历操作原理(一)

    Redis在2.8.0版本新增了众望所归的scan操作,从此再也不用担心敲入了keys*, 然后举起双手看着键盘等待漫长的系统卡死了··· 命令的官方介绍在这里, 中文版由huangz同学细心翻译了, ...

  5. PID控制算法的C语言实现九

    (1)微分先行PID控制算法 微分先行PID控制的特点是只对输出量yout(k)进行微分,而对给定值rin(k)不进行微分.这样,在改变给定值时,输出不会改变,而被控量的变化通常是比较缓和的.这种输出 ...

  6. java面试之闭包(closure)

    今天在学习Openresty的时候回顾了下闭包这个问题,感觉很久没有深入的了解这块的内容的,只是之前js的时候学习过闭包,突然一问,感觉不记得闭包了: 看了一个比较有趣的答案: 闭包,顾名思义,就是把 ...

  7. Python2和Python3共存安装

    记录下: 先下载Python2.7.6,安装完成,不要添加到path中: 再下载Python3.4.3,安装,不要添加到path中. 进入 Python2: py -2 进入Python3: py - ...

  8. Qt ------ QTabWidget

    下图: 1.长方形的 objectName 可写可不写,不写就作用于所有 QTabWidget:椭圆形的 QTabWidget#tabWidget 要么四个都要写,要么四个都不写 2.下图的 CSS ...

  9. tp between

    $a = array( 'time' => array('between',[c,d]) ); c<= time <= d

  10. Scratch编程小案例:愤怒的小牛

    愤怒的小鸟曾经很热门,网上还说他是程序员最喜欢玩的游戏.最先我是WIKIOI的评测页面看到他的,后来在2014年全国信息学奥林匹克联赛第一天第三题飞扬的小鸟也看到了它.因此,突然想做一个类似愤怒的小鸟 ...