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. Javascript高级编程学习笔记(43)—— 动态脚本

    动态脚本 大多数情况下,DOM操作都很简洁明了 因为DOM主要就是用来操作页面中的可视节点的 但有些时候我们又希望可以动态的来进行DOM操作 其中的一部分也就是今天我们的内容动态脚本 动态脚本是什么意 ...

  2. 查看windows到期时间

        查看windows到期时间    Slmgr.vbs  -xpr

  3. Mysql实现null值排在最前或最后

    最近在做项目迁移,Oracle版本的迁到Mysql版本,遇到有些oracle的函数,mysql并没有,所以就只好想自定义函数或者找到替换函数的方法进行改造. oracle做数据排序的时候,有时候可以用 ...

  4. 性能调优之Mapping

    Mapping层级的调优可能会花费时间,但是性能调优的效果确实非常显著的 优化Target,Source之后,可以调优Mapping 通常的方法是尽可能减少组件及组件的字段间不必要的连线 即尽可能用最 ...

  5. requests 处理异常错误 requests.exceptions.ConnectionError HTTPSConnectionPool [Errno 10060]

    使用python requests模块调用vmallarg.vmall.com接口API时报如下错误: requests.exceptions.ConnectionError: HTTPSConnec ...

  6. 整理+学习《骆昊-Java面试题全集(中)》

    这部分主要是与Java Web和Web Service相关的面试题. 96.阐述Servlet和CGI的区别? 答:Servlet与CGI的区别在于Servlet处于服务器进程中,它通过多线程方式运行 ...

  7. odoo开发笔记--取消正在升级中模块

    场景描述: 开发过程中,有时候会遇到,模块安装的时候,一直卡着,走不过去:或者安装报错,导致进入了系统! 这个时候,怎么处理呢? 先简单说下,odoo中模块的升级有4个状态,模块相关的安装情况记录在表 ...

  8. Spring boot集成spring-boot-starter-data-jpa环境搭建

    1.创建Spring boot项目 2.保存等待构建完成 3.增加spring-boot-starter-data-jpa.内存数据库依赖包hsqldb <!-- 添加data jpa依赖 -- ...

  9. jdk8 分隔字符串最新方法

    //已字符串分隔方法最新 方法 StringJoiner stringJoiner=new StringJoiner(","); stringJoiner.add("a& ...

  10. Mysql 调优和水平扩展思路

    系统调优参数 一些比较重要的参数: back_log:back_log值指出在MySQL暂时停止回答新请求之前的短时间内多少个请求可以被存在堆栈中.如果MySql的连接数据达到max_connecti ...