Spring Boot可以和大部分流行的测试框架协同工作:通过Spring JUnit创建单元测试;生成测试数据初始化数据库用于测试;Spring Boot可以跟BDD(Behavier Driven Development)工具、Cucumber和Spock协同工作,对应用程序进行测试。

进行软件开发的时候,我们会写很多代码,不过,再过六个月(甚至一年以上)你知道自己的代码怎么运作么?通过测试(单元测试、集成测试、接口测试)可以保证系统的可维护性,当我们修改了某些代码时,通过回归测试可以检查是否引入了新的bug。总得来说,测试让系统不再是一个黑盒子,让开发人员确认系统可用。

在web应用程序中,对Controller层的测试一般有两种方法:(1)发送http请求;(2)模拟http请求对象。第一种方法需要配置回归环境,通过修改代码统计的策略来计算覆盖率;第二种方法是比较正规的思路,但是在我目前经历过的项目中用得不多,今天总结下如何用Mock对象测试Controller层的代码。

在之前的几篇文章中,我们都使用bookpub这个应用程序作为例子,今天也不例外,准备测试它提供的RESTful接口是否能返回正确的响应数据。这种测试不同于单元测试,需要为之初始化完整的应用程序上下文、所有的spring bean都织入以及数据库中需要有测试数据,一般来说这种测试称之为集成测试或者接口测试

mvc测试代码: 

package com.letv.bigdata.idfa.test.controller;

import com.letv.bigdata.idfa.controller.IdfaController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner; import org.junit.*;
import org.junit.runner.*;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext; @RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
@WebAppConfiguration
public class IdfaControllerTest { private MockMvc mvc;
@Autowired
private WebApplicationContext context; @Test
public void test() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/query"))
.andExpect(MockMvcResultMatchers.status().isOk());
// .andExpect(MockMvcResultMatchers.content().string("abc"));
} @Before
public void setupMockMvc() throws Exception {
mvc = MockMvcBuilders.webAppContextSetup(context).build();
} }

可以进行简单的get测试, 其他待测

 

springboot-32-使用mvc测试的更多相关文章

  1. springBoot单元测试-模拟MVC测试

    1)模拟mvc测试,和基础测试是一样的, 都需要在pom文件中引入junit的支持. 略 2)编写测试类 Application1TestMVC 在类头上除啦加入之前的@RunWith(SpringR ...

  2. 【深度分析】:阿里,腾讯面试题 SpringBoot整合Spring MVC

    Java学习总结 SpringBoot整合Spring MVC 1.SpringMVC概述 MVC(Model–view–controller)是软件工程中的一种软件架构模式,基于此模式把软件系统分为 ...

  3. SpringBoot学习之mvc

    Spring Boot非常适合Web应用程序开发. 我们可以使用嵌入式Tomcat,Jetty或Undertow轻松创建自包含的HTTP服务器. 大多数Web应用程序将使用spring-boot-st ...

  4. springboot区分开发、测试、生产多环境的应用配置(二)

    转:https://www.jb51.net/article/139119.htm springboot区分开发.测试.生产多环境的应用配置(二) 这篇文章主要给大家介绍了关于maven profil ...

  5. springboot区分开发、测试、生产多环境的应用配置

    转:https://blog.csdn.net/daguairen/article/details/79236885 springboot区分开发.测试.生产多环境的应用配置(一) Spring可使用 ...

  6. Spring MVC测试框架

    原文链接:http://jinnianshilongnian.iteye.com/blog/2004660 Spring MVC测试框架详解——服务端测试 博客分类: springmvc杂谈 spri ...

  7. Spring MVC测试框架详解——服务端测试

    随着RESTful Web Service的流行,测试对外的Service是否满足期望也变的必要的.从Spring 3.2开始Spring了Spring Web测试框架,如果版本低于3.2,请使用sp ...

  8. 从Spring到SpringBoot构建WEB MVC核心配置详解

    目录 理解Spring WEB MVC架构的演变 认识Spring WEB MVC 传统时代的Spring WEB MVC 新时代Spring WEB MVC SpringBoot简化WEB MVC开 ...

  9. SpringBoot项目构建、测试、热部署、配置原理、执行流程

    SpringBoot项目构建.测试.热部署.配置原理.执行流程 一.项目构建 二.测试和热部署 三.配置原理 四.执行流程

随机推荐

  1. (单调队列) Bad Hair Day -- POJ -- 3250

    http://poj.org/problem?id=3250 Bad Hair Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissi ...

  2. js获取标签下标

    <body> <div class="titTab"> <span>低</span> <span>中</span& ...

  3. Codeforces Round #540 (Div. 3)--1118B - Tanya and Candies(easy TL!)

    Tanya has nn candies numbered from 11 to nn. The ii-th candy has the weight aiai. She plans to eat e ...

  4. 20155326 2006-2007-2 《Java程序设计》第4周学习总结

    20155326 2006-2007-2 <Java程序设计>第4周学习总结 教材学习内容总结 继承共同行为 (1)继承基本上就是避免多个类间重复定义共同行为,关键词为extends. ( ...

  5. bootstrap1.3

    <html>   <head>   <meta charset="UTF-8">   <title></title>   ...

  6. Python:windows下scikit-learn 安装和更新

    scikit-learn 报错: from sklearn.model_selection import train_test_split ImportError: No module named m ...

  7. C# 子线程调用主线程窗体的解决方法

    摘自其他人博客,自己试过确实解决问题.(如在自己定义的线程里面给textbox赋值) 由于Windows窗体控件本质上不是线程安全的.因此如果有两个或多个线程适度操作某一控件的状态(set value ...

  8. JS文件中的中文在网页引用时显示乱码的简单解决方式

    今天把一个jquery方法从前台cshtml文件转移到单独的js文件中后执行不成功,调试发现if判断中的中文字符串变成了乱码,之前在前台文件中是可以正常显示的,所以判定可能是跟文件的编码方式有关系. ...

  9. Windows下安装配置爬虫工具Scrapy及爬虫环境

    爬虫工具Scrapy在Mac和Linux环境下都相对好装,但是在Windows上总会碰到各种莫名其妙的问题.本文记录下Scrapy在Window上的安装过程. 本文是基于Python2.7及Windo ...

  10. <转>php中heredoc与nowdoc的使用方法

    http://www.361way.com/php-heredoc-nowdoc/3008.html 一.heredoc结构及用法 Heredoc 结构就象是没有使用双引号的双引号字符串,这就是说在 ...