springboot之单元测试
springboot在写完之后,肯定都需要进行单元测试,如下给出一些样例
工程层次结构如图

代码如下:
controller:
package com.rookie.bigdata.controller; import com.rookie.bigdata.domain.User;
import com.rookie.bigdata.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; /**
* controller层
* Created by on 2018/9/28.
*/
@RestController
public class UserController { @Autowired
private UserService userService; /**
* 查询用户
*
* @return
*/
@GetMapping(value = "/user")
public User findUser() {
return userService.findOne(10);
} }
User:
package com.rookie.bigdata.domain; /**
* domain实体对象
* Created by on 2018/9/28.
*/
public class User {
private int id;
private String name;
private Integer age; public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} public User() {
} public int getId() {
return id;
} public void setId(int id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} @Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
service:
package com.rookie.bigdata.service; import com.rookie.bigdata.domain.User;
import org.springframework.stereotype.Service; /**
* service层
* Created by on 2018/9/28.
*/
@Service
public class UserService { public User findOne(Integer id) {
User user = new User();
user.setId(id);
user.setName("张三");
user.setAge(23);
return user;
}
}
启动程序:
package com.rookie.bigdata; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling; /**
* 应用程序启动类
* Created by on 2018/8/2.
*/
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args); }
}
测试类:
package com.rookie.bigdata.controller; 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.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers; import static org.junit.Assert.*; /**
* 测试contoller层
* Created by on 2018/9/28.
*/
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class UserControllerTest { @Autowired
private MockMvc mvc; @Test
public void findUser() throws Exception {
// mvc.perform(MockMvcRequestBuilders.get("/user"))
// .andExpect(MockMvcResultMatchers.status().isOk()); MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get("/user"))
.andExpect(MockMvcResultMatchers.status().isOk())//模拟发送get请求
.andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_UTF8))//预期返回值的媒体类型 application/json;charset=UTF-8
.andReturn();//返回执行的请求结果 System.out.println(mvcResult.getResponse().getContentAsString()); } }
package com.rookie.bigdata.service; import com.rookie.bigdata.domain.User;
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; /**
* 测试service层
* Created by on 2018/9/28.
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserServiceTest { @Autowired
private UserService userService; @Test
public void findOne() throws Exception { User user = userService.findOne(1); Assert.assertEquals(new Integer(23),user.getAge());
} }
springboot之单元测试的更多相关文章
- SpringBoot系列: 单元测试2
之前发了SpringBoot 单元测试的博客, https://www.cnblogs.com/harrychinese/p/springboot_unittesting.html , 内容较少, 现 ...
- SpringBoot系列: 单元测试
SpringBoot 项目单元测试也很方便, Web项目中单元测试应该覆盖:1. Service 层2. Controller 层 本文前半部分讲解是一些测试基础配置. 对于Service和Contr ...
- SpringBoot项目单元测试
关于SpringBoot的单元测试,描述一下三种单元测试的方式. 1.约定 单元测试代码写在src/test/java目录下单元测试类命名为*Test,前缀为要测试的类名 2. 使用mock方式单元测 ...
- 【SpringBoot】单元测试进阶实战、自定义异常处理、t部署war项目到tomcat9和启动原理讲解
========================4.Springboot2.0单元测试进阶实战和自定义异常处理 ============================== 1.@SpringBoot ...
- SpringBoot进行单元测试
SpringBoot进行单元测试,需要在maven中加入以下依赖 <dependency> <groupId>org.springframework.boot</grou ...
- springboot的单元测试(总结两种)
.personSunflowerP { background: rgba(51, 153, 0, 0.66); border-bottom: 1px solid rgba(0, 102, 0, 1); ...
- springBoot(5)---单元测试,全局异常
单元测试,全局异常 一.单元测试 1.基础版 1.引入相关依赖 <!--springboot程序测试依赖,如果是自动创建项目默认添加--> <dependency> <g ...
- ABAP和Java SpringBoot的单元测试
ABAP 在ABAP类里,本地类(Local Class)里用关键字FOR TESTING声明过的方法, 在单元测试启动后会自动被调用到. Spring Boot 在Spring及Spring Boo ...
- Springboot整合单元测试
概述 对于简单易懂的小项目而言,可以不适用单元测试对平时开发没有什么影响,但是对于大型项目,单纯的依赖 “手点功能测试”, 那简直就是灾难,好了,说道这里,应该明白测试的一个重要性了,,,接下来,我们 ...
随机推荐
- 【Vue】谈Vue的依赖追踪系统 ——搞懂methods watch和compute的区别和联系
从作用机制和性质上看待methods,watch和computed的关系 图片标题[原创]:<他三个是啥子关系呢?> 首先要说,methods,watch和computed都是以函数为基础 ...
- vs2012升级vs2017后的一些坑
异常信息:未能加载文件或程序集"System.Web.Helpers... 未能加载文件或程序集"System.Web.Helpers, Version=2.0.0.0, Cult ...
- Akka-CQRS(6)- read-side
前面我们全面介绍了在akka-cluster环境下实现的CQRS写端write-side.简单来说就是把发生事件描述作为对象严格按发生时间顺序写入数据库.这些事件对象一般是按照二进制binary方式如 ...
- 解决mysql插入数据报错[Err] 1146 - Table 'performance_schema.session_status' doesn't exist
解决办法:1.打开cmd 执行命令cd/ 进入C盘根目录2.dir 查看C盘根目录下文件夹 找到 Program Files文件夹3.cd Program Files 进入该文件夹下 再输入dir ...
- 【详记MySql问题大全集】二、安装并破解Navicat
Navicat for MySql 11.1.13 企业版 下载地址: 链接:https://pan.baidu.com/s/1N3ZQXNyx-W8D4AsuZdsMug 密码:x0rd 第二个是N ...
- Oracle的nvl函数和nvl2函数
一.基本语法 介绍一下oracle的nvl函数和nvl2函数. nvl函数 nvl函数基本语法为nvl(E1,E2),意思是E1为null就返回E2,不为null就返回E1. nvl2函数 nvl2函 ...
- [干货,阅后进BAT不是梦]面试心得与总结---阿里、小米、腾讯
之前实习的时候就想着写一篇面经,后来忙就给忘了,现在找完工作了,也是该静下心总结一下走过的路程了,我全盘托出,奉上这篇诚意之作,希望能给未来找工作的人一点指引和总结, 也希望能使大家少走点弯路 , 如 ...
- 图片base64上传时可能遇到的问题
base64上传图片时服务器接到的值可能会丢失字符串 解决方法如下:(分为单个上传和多个上传) <?php $BASE_DIR = "../"; //文件上传 $img = ...
- Eclipse java文件、包、工程左下角有感叹号原因及处理方法
先想如图所示: 原因:多是import引用了无用的类,或集合没指定元素的类型 解决方案如下: 1.可逐一到出现感叹号的java文件中去处理 2.在java文件中使用unchecked,如@Suppre ...
- Android UI(一)Layout 背景局部Shape圆角设计
Jeff Lee blog: http://www.cnblogs.com/Alandre/ (泥沙砖瓦浆木匠),retain the url when reproduced ! Thanks ...