进行单元测试:

service第一种方式:

第一步:在指定service中创建一个方法进行测试

 /**
* 通过ID查询一个女生的信息
* @param id
* @return
*/
public Girl findOne(Integer id){
return girlRespository.findOne(id);
}

第二步:在test文件夹下指定的包中创建GirlServiceTest

package com.payease;

import com.payease.domain.Girl;
import com.payease.service.GirlService;
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 liuxiaoming on 2017/11/8.
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class GirlServiceTest{ @Autowired
private GirlService girlService; @Test
public void findOneTest(){
Girl girl = girlService.findOne(13);
Assert.assertEquals((Object) new Integer(20), girl.getAge());
} }

第三步:查看数据库信息  启动测试类

测试通过:

若下图原数字20改为21:

测试结果:

 service第二种方式:

第一步:在service中找到该方法。 鼠标右键 选择 go to--test--Create New Test. . .--勾选你所要测试的方法--OK

第二步:点击OK后 在test目录中自动生成包和文件

controller单元测试:

第一步:找到对应 controller中的将要测试的方法 点击鼠标右键 选择 go to--test--Create New Test. . .--勾选你所要测试的方法--OK

第二步:点击OK后 在test目录中自动生成包和文件

第三步:编写GirlControllerTest 期望返回状态: .andExpect(MockMvcResultMatchers.status().isOk());

package com.payease.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.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; /**
* controller测试类
* Created by liuxiaoming on 2017/11/8.
*/
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class GirlControllerTest { @Autowired
private MockMvc mvc; @Test
public void testGirlList() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/girls")) //请求方法方式和请求名称
.andExpect(MockMvcResultMatchers.status().isOk()); //请求返回状态码比对
}
}

第四步:运行该测试类

测试成功:

注:修改GirlControllerTest /girls 改为 /girlss 路径不存在

测试结果:

注:修改GirlControllerTest  新增期望返回内容判断:.andExpect(MockMvcResultMatchers.content().string("abc"));

测试结果:

注:在终端对项目进行打包时 会自动进行单元测试

测试成功的结果:

注:跳过单元测试直接打包命令: mvn clean package -Dmaven.test.skip=true

测试结果:

spring boot快速入门 9: 单元测试的更多相关文章

  1. Spring Boot 快速入门

    Spring Boot 快速入门 http://blog.csdn.net/xiaoyu411502/article/details/47864969 今天给大家介绍一下Spring Boot MVC ...

  2. Spring Boot快速入门(二):http请求

    原文地址:https://lierabbit.cn/articles/4 一.准备 postman:一个接口测试工具 创建一个新工程 选择web 不会的请看Spring Boot快速入门(一):Hel ...

  3. spring boot入门教程——Spring Boot快速入门指南

    Spring Boot已成为当今最流行的微服务开发框架,本文是如何使用Spring Boot快速开始Web微服务开发的指南,我们将使创建一个可运行的包含内嵌Web容器(默认使用的是Tomcat)的可运 ...

  4. Spring Boot 快速入门 史上最简单

    1.Spring Boot 概述 Spring Boot 是所有基于 Spring 开发的项目的起点.Spring Boot 的设计是为了让你尽可能快的跑起来 Spring 应用程序并且尽可能减少你的 ...

  5. Spring Boot 快速入门(IDEA)

    从字面理解,Boot是引导的意思,因此SpringBoot帮助开发者快速搭建Spring框架:SpringBoot帮助开发者快速启动一个Web容器:SpringBoot继承了原有Spring框架的优秀 ...

  6. 笔记61 Spring Boot快速入门(一)

    IDEA+Spring Boot快速搭建 一.IDEA创建项目 略 项目创建成功后在resources包下,属性文件application.properties中,把数据库连接属性加上,同时可以设置服 ...

  7. Spring Boot快速入门(最新)

    本章通过完成Spring Boot基础项目的构建并实现一个简单的Http请求处理,让大家对Spring Boot有一个初步的了解,并体验其结构简单.开发快速的特性.预计阅读及演练过程将花费约5分钟. ...

  8. Spring Boot 快速入门笔记

    Spirng boot笔记 简介 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发 ...

  9. Spring Boot快速入门(三):依赖注入

    原文地址:https://lierabbit.cn/articles/6 spring boot使用依赖注入的方式很简单,只需要给添加相应的注解即可 @Service用于标注业务层组件 @Contro ...

随机推荐

  1. linux计划任务(二)

    计划任务的授权 1.at任务 /etc/at.allow /etc/at.deny 2.crontab任务 /etc/cron.allow /etc/cron.deny [注:如果allow文件存在, ...

  2. 在iOS项目中引入MVVM

    本文翻译自:http://www.objc.io/issue-13/mvvm.html.为了方便读者并节约时间,有些不是和文章主题相关的就去掉了.如果读者要看原文的话可以通过前面的url直接访问.作者 ...

  3. Delphi获取文件名、文件名不带扩展名、文件名的方法;delphi 获取文件所在路径

    取文件名 ExtractFileName(FileName); 取文件扩展名: ExtractFileExt(filename); 取文件名,不带扩展名: 方法一:   Function Extrac ...

  4. Android-Gson解析JSON数据(JSON对象/JSON数组)

    上一篇博客,Android-解析JSON数据(JSON对象/JSON数组),介绍了使用 org.json.JSONArray;/org.json.JSONObject; 来解析JSON数据: Goog ...

  5. 国际时区 TimeZone ID列表

    public static void main(String[] args) { Calendar c = new GregorianCalendar(); c.setTime(new Date()) ...

  6. apache mpm的一些问题

    win2003系统下apache环境,mpm_winnt.c模式,优化参数: ThreadsPerChild 说明:每个子进程建立的线程数,默认值:64,最大值:1920.网上查询资料建议设置在100 ...

  7. 在定制工作项时,把“团队项目”作为变量获取生成版本信息

    有用户最近提出这个需求: 通过工作项定制,新增一个字段用以保存项目Bug的"影响版本"信息,但是需要从当前团队项目的服务器生成纪录中获取版本的选项,类似默认模板中的"发现 ...

  8. File Path Directory总结

    阅读目录 开始 Path 对路径 字符串进行操作 获得后缀 能合并路径 获取文件名 Directory和DirectoryInfo  对目录进行操作 判断目录是否存在 创建目录 删除目录 获取目录下所 ...

  9. c# 利用t4模板,自动生成Model类

    我们在用ORM(比如dapper)的时候,很多时候都需要自己写Model层(当然也有很多orm框架自带了这种功能,比如ef),特别是表里字段比较多的时候,一个Model要写半天,而且Model如果用于 ...

  10. 设置TeeChart的提示文本

    使用第三方Steema的TeeChart控件,设置鼠标放在某一线条点上,显示某一点的数据标签问题(虚线型十字光标基准线,放在线上显示对应点的二维坐标轴数据数据),调用InitTeeChartTipTo ...