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之单元测试的更多相关文章

  1. SpringBoot系列: 单元测试2

    之前发了SpringBoot 单元测试的博客, https://www.cnblogs.com/harrychinese/p/springboot_unittesting.html , 内容较少, 现 ...

  2. SpringBoot系列: 单元测试

    SpringBoot 项目单元测试也很方便, Web项目中单元测试应该覆盖:1. Service 层2. Controller 层 本文前半部分讲解是一些测试基础配置. 对于Service和Contr ...

  3. SpringBoot项目单元测试

    关于SpringBoot的单元测试,描述一下三种单元测试的方式. 1.约定 单元测试代码写在src/test/java目录下单元测试类命名为*Test,前缀为要测试的类名 2. 使用mock方式单元测 ...

  4. 【SpringBoot】单元测试进阶实战、自定义异常处理、t部署war项目到tomcat9和启动原理讲解

    ========================4.Springboot2.0单元测试进阶实战和自定义异常处理 ============================== 1.@SpringBoot ...

  5. SpringBoot进行单元测试

    SpringBoot进行单元测试,需要在maven中加入以下依赖 <dependency> <groupId>org.springframework.boot</grou ...

  6. springboot的单元测试(总结两种)

    .personSunflowerP { background: rgba(51, 153, 0, 0.66); border-bottom: 1px solid rgba(0, 102, 0, 1); ...

  7. springBoot(5)---单元测试,全局异常

    单元测试,全局异常 一.单元测试 1.基础版 1.引入相关依赖 <!--springboot程序测试依赖,如果是自动创建项目默认添加--> <dependency> <g ...

  8. ABAP和Java SpringBoot的单元测试

    ABAP 在ABAP类里,本地类(Local Class)里用关键字FOR TESTING声明过的方法, 在单元测试启动后会自动被调用到. Spring Boot 在Spring及Spring Boo ...

  9. Springboot整合单元测试

    概述 对于简单易懂的小项目而言,可以不适用单元测试对平时开发没有什么影响,但是对于大型项目,单纯的依赖 “手点功能测试”, 那简直就是灾难,好了,说道这里,应该明白测试的一个重要性了,,,接下来,我们 ...

随机推荐

  1. 一篇入门 — Scala 宏

    前情回顾 上一节, 我简单的说了一下反射的基本概念以及运行时反射的用法, 同时简单的介绍了一下编译原理知识, 其中我感觉最为绕的地方, 就属泛型的几种使用方式了. 而最抽象的概念, 就是对于符号和抽象 ...

  2. Xftp5软件使用详解

    一.首先运行Xftp5,然后导航栏上面有个小加号,点击进去. 二.接着出现如下界面,在这里填写名称(这个随意填写),主机填写要连接的主机的IP地址,然后协议的话,Linux系统一般选择SFTP协议,端 ...

  3. ElasticSearch是如何实现分布式的?

    面试题 es 的分布式架构原理能说一下么(es 是如何实现分布式的啊)? 面试官心理分析 在搜索这块,lucene 是最流行的搜索库.几年前业内一般都问,你了解 lucene 吗?你知道倒排索引的原理 ...

  4. Javascript高级编程学习笔记(22)—— 对象继承

    继承是所有面向对象的语言最让人津津乐道的概念 许多面向对象的语言都支持两种实现继承的方式: 1.接口继承 2.实现继承 由于ECMAScript中没有函数签名,所以自然也是不支持接口继承 所以JS中能 ...

  5. 吴恩达机器学习笔记37-学习曲线(Learning Curves)

    学习曲线就是一种很好的工具,我经常使用学习曲线来判断某一个学习算法是否处于偏差.方差问题.学习曲线是学习算法的一个很好的合理检验(sanity check).学习曲线是将训练集误差和交叉验证集误差作为 ...

  6. JDK设计模式之——策略模式(Comparable和Comparator接口)

    策略模式:其实就是java的多态...父类引用指向子类对象. 使用策略模式,改善排序算法上文中需要排序的是一个数组 让他可以对任何类型的数组进行排序 1.利用 接口 Comparable<T&g ...

  7. less编译工具——koala使用介绍

    1:使用koala编译软件    官网:http://koala-app.com/index-zh.html (目前官网点击下载没有反应,有人说可能是网络问题,但真正的原因是需要FQ才能下载了) 百度 ...

  8. 【spring boot】idea下springboot打包成jar包和war包,并且可以在外部tomcat下运行访问到(转)

    转自:https://www.cnblogs.com/sxdcgaq8080/p/7727249.html   接着上一章走呗:http://www.cnblogs.com/sxdcgaq8080/p ...

  9. .Net 环境下比较各种数据库插入操作的性能

    1.简介 再说Windows的异步I/O操作前,先聊聊一些题外话,能帮助我们更好的理解异步I/O操作,常规的Web程序,当用户发起一次请求,当请求通过管道到达客户端的这个过程,会唤起一个线程池线程(后 ...

  10. stack源码

    stack概述 stack是一种先进后出的数据结构,它只有一个出口,允许新增元素.移除元素.取得最顶端元素,但每次只能处理顶端元素,也就是说,stack不允许遍历行为. stack定义 以某种既有容器 ...