16-SpringBoot——Spring MVC基础-测试
来源:互联网 发布:网络购物流程 编辑:程序博客网 时间:2024/06/06 04:12
Spring MVC基础-测试
【博文目录>>>】
【项目源码>>>】
【测试】
测试是保证软件质量的关键,为了测试Web 项目通常不需要启动项目,我们需要一些Servlet 相关的模拟对象,比如:MockMVC 、MockHttpServletRequest 、MockHttpServletResponse 、MockHttpSession 等。在Spring 里,我们使用@WebAppConfiguration 指定加载的ApplicationContext 是一个WebApplicationContext 。通过他来模拟一个web测试环境。Spring提供了非常丰富的测试功能。本例就演示如何使用Spring测试。
【代码实现】
package com.example.spring.framework.test.service;import org.springframework.stereotype.Service;/** * Author: 王俊超 * Date: 2017-07-14 08:14 * All Rights Reserved !!! */@Servicepublic class DemoService { public String saySomething() { return "hello"; }}
package com.example.spring.framework.test.controller;import com.example.spring.framework.test.service.DemoService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;/** * Author: 王俊超 * Date: 2017-07-14 20:42 * All Rights Reserved !!! */@Controllerpublic class NormalController { @Autowired DemoService demoService; @RequestMapping(value = "/normal", produces = "text/plain;charset=UTF-8") public String testPage(Model model) { model.addAttribute("msg", demoService.saySomething()); return "page"; }}
package com.example.spring.framework.test.controller;import com.example.spring.framework.test.service.DemoService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.bind.annotation.RestController;/** * Author: 王俊超 * Date: 2017-07-14 20:45 * All Rights Reserved !!! */@RestControllerpublic class MyRestController { @Autowired DemoService demoService; @RequestMapping(value = "/testRest", produces = "text/plain;charset=UTF-8") public @ResponseBody String testRest() { return demoService.saySomething(); }}
package com.example.spring.framework.test.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.scheduling.annotation.EnableScheduling;import org.springframework.web.servlet.config.annotation.EnableWebMvc;import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;import org.springframework.web.servlet.view.InternalResourceViewResolver;import org.springframework.web.servlet.view.JstlView;/** * Author: 王俊超 * Date: 2017-07-14 08:15 * All Rights Reserved !!! */@Configuration@EnableWebMvc@EnableScheduling@ComponentScan("com.example.spring.framework.test")public class MyMvcConfig extends WebMvcConfigurerAdapter { @Bean public InternalResourceViewResolver viewResolver() { InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setPrefix("/WEB-INF/classes/views/"); viewResolver.setSuffix(".jsp"); viewResolver.setViewClass(JstlView.class); return viewResolver; } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/assets/**").addResourceLocations("classpath:/assets/"); super.addResourceHandlers(registry); } @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/see").setViewName("/see"); registry.addViewController("/async").setViewName("/async"); }}
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Test page</title></head><body><pre> Welcome to Spring MVC world</pre></body></html>
package com.example.spring.framework.test;import com.example.spring.framework.test.config.MyMvcConfig;import com.example.spring.framework.test.service.DemoService;import org.junit.Before;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.mock.web.MockHttpServletRequest;import org.springframework.mock.web.MockHttpSession;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringRunner;import org.springframework.test.context.web.WebAppConfiguration;import org.springframework.test.web.servlet.MockMvc;import org.springframework.test.web.servlet.setup.MockMvcBuilders;import org.springframework.web.context.WebApplicationContext;import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;/** * 1、@WebAppConfiguration 注解在类上,用来声明加载的ApplicationContex 是一个WebApplicationContext * 它的属性指定的是Web 资源的位置,默认为src/main/webapp ,本例修改为src/main/resources * 2、MockMvc模拟MVC对象 * 3、可以在测试用例中注入Spring 的Bean * Author: 王俊超 * Date: 2017-07-14 08:17 * All Rights Reserved !!! */@RunWith(SpringRunner.class)@ContextConfiguration(classes = MyMvcConfig.class)@WebAppConfiguration("src/main/resources")public class TestControllerIntegrationTests { private MockMvc mockMvc; @Autowired private DemoService demoService; @Autowired private WebApplicationContext wac; @Autowired MockHttpSession session; @Autowired private MockHttpServletRequest request; @Before public void setup() { mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); } @Test public void testNormalController() throws Exception { mockMvc.perform(get("/normal")) .andExpect(status().isOk()) .andExpect(view().name("page")) .andExpect(forwardedUrl("/WEB-INF/classes/views/page.jsp")) .andExpect(model().attribute("msg", demoService.saySomething())); } @Test public void testRestController() throws Exception { mockMvc.perform(get("/testRest")) .andExpect(status().isOk()) .andExpect(content().contentType("text/plain;charset=UTF-8")) .andExpect(content().string(demoService.saySomething())); }}
【运行结果】
阅读全文
2 0
- 16-SpringBoot——Spring MVC基础-测试
- 11-SpringBoot——Spring MVC基础-常用注解
- 12-SpringBoot——Spring MVC基础-常用配置
- 14-SpringBoot——Spring MVC基础-自定义HttpMessageConverter
- 15-SpringBoot——Spring MVC基础-服务端推送技术
- 13-SpringBoot——Spring MVC基础-高级配置-文件上传
- 03-SpringBoot——Spring基础-AOP
- 01-SpringBoot——Spring基础-依赖注入
- 02-SpringBoot——Spring基础-Java配置
- Spring MVC测试框架详解——服务端测试
- Spring MVC测试框架详解——服务端测试
- Spring MVC测试框架详解——服务端测试
- Spring MVC测试框架详解——服务端测试
- Spring MVC测试框架详解——服务端测试
- Spring MVC测试框架详解——服务端测试
- Spring MVC测试框架详解——服务端测试
- Spring MVC测试框架详解——服务端测试
- Spring MVC测试框架详解——服务端测试
- python中的全局变量和局部变量
- 测试六 赛后感受
- 入门Laravel的笔记
- 递归
- (二分+模拟)GYM_100253 Travelling Camera Problem
- 16-SpringBoot——Spring MVC基础-测试
- RMQ算法
- 23种设计模式汇总整理
- python入门(十六):time模块
- 2.4 进度显示类控件的使用
- Oracle基本数据类型(纸质笔记怕弄丢而已)
- 2.5 Dialog 对话框类控件的使用
- 设计模式之禅笔记--面向对象设计六大原则之四
- 2.6 通知类控件 Toast、Menu