美文网首页
Spring框架如何测试

Spring框架如何测试

作者: 刘军颖 | 来源:发表于2019-05-27 15:27 被阅读0次

Spring框架如何测试

过去写单独文件的时候我们经常使用的是JUNIT标注来实现一个方法的测试,但是当我们使用WEBMVC进行程序代码编写的时候我们已经无法使用了过去那种测试了,这个需求Spring已经帮我们想到了,提供了优秀的库Spring-test帮助我们。

必备知识点

@RunWith

用于指定junit运行环境,是junit提供给其他框架测试环境接口扩展,为了便于使用spring的依赖注入,spring提供了org.springframework.test.context.junit4.SpringJUnit4ClassRunner作为Junit测试环境,例如:@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration

导入配置文件,例如:@ContextConfiguration(classes = { MyMvcConfig.class })

@Transactional

@WebAppConfiguration

WebAppConfiguration 注解在类上, 用来声明加载的ApplicationContex是一个 WebApplicationContext。 它的属性指定的是Web资源的位置,默认为src/main/webapp

@Before

在测试开始前进行的初始化工作

MockMvc

MockMvc 模拟MVC对象, 通过 MockMvcBuilders.webAppContextSetup(this.wac).build() 初始化。

MockHttpSession

可注入模拟的http session

MockHttpServletRequest

可注入模拟的 http request

mockMvc.perform(get("/normal"))

模拟向/normal进行get请求。

.andExpect(status().isOk())

返回状态码

.andExpect(view().name("demo"))

设置返回view的名称为demo

.andExpect(forwardedUrl("/WEB-INF/classes/views/demo.jsp"))

指定view存放的路径

.andExpect(model().attribute("msg",demoService.demo()))

预期 model 里 的 值 是 demoService. saySomething()返回 值 hello。

.andExpect(content().contentType("text/plain;charset=UTF-8"))

返回 值 的 媒体 类型 为text/plain;charset=UTF-8

.andExpect(content().string(demoService.saySomething()))

预期 返回 值 的 内容 为demoService.demo()

实际操作

正常的控制器

package com.haojishu.demo.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.haojishu.demo.service.DemoService;

@Controller
public class HelloController {

    @Autowired
    DemoService demoService;

    @RequestMapping("/index")
    public String hello(Model model) {
        model.addAttribute("msg", demoService.demo());
        return "index";
    }

}

测试控制器

package com.haojishu.demo;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;

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.SpringJUnit4ClassRunner;
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 com.haojishu.demo.service.DemoService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { MyMvcConfig.class })
@WebAppConfiguration("src/main/resource")
public class TestController {
    private MockMvc mockMvc;

    @Autowired
    private DemoService demoService;

    @Before
    public void setup() {
        mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
    }

    @Test
    public void testIndexController() throws Exception {
                     mockMvc.perform(get("/index")).andExpect(status().isOk()).andExpect(view().name("index"))
                .andExpect(forwardedUrl("/WEB-INF/classes/views/index.jsp"))
                .andExpect(model().attribute("msg", demoService.demo()));
    }
}

相关文章

  • Spring中使用单元测试SpringTest

    使用Spring测试套件后,代码是如何变优雅的。 1. 加入依赖包 使用spring的测试框架需要加入以下依赖包:...

  • Spring框架如何测试

    Spring框架如何测试 过去写单独文件的时候我们经常使用的是JUNIT标注来实现一个方法的测试,但是当我们使用W...

  • Spring Web MVC框架(十一) Spring Web

    Spring 也提供了完善的测试框架,我们可以方便的测试Spring Web MVC应用程序。为了使用这个测试框架...

  • 引入jasmine测试框架

    引入jasmine测试框架 1、下载jasmine测试框架 2、Spring Boot项目引入jasmine测试框...

  • Spring Boot测试

    Spring Boot测试 Spring 提供的了一个test框架,用于系统的集成测试。在Spring Boot中...

  • spring02

    Spring相关注解 Spring注解开发 集成Spring测试框架 重点:重点掌握Spring相关注解。@Con...

  • Spring Boot应用的测试——Mockito

    Spring Boot可以和大部分流行的测试框架协同工作:通过Spring JUnit创建单元测试;生成测试数据初...

  • SpringMVC 测试 mockMVC

    SpringMVC测试框架 基于RESTful风格的SpringMVC的测试,我们可以测试完整的Spring MV...

  • spring核心内容理解

    概述 什么是Spring?(what) 如何使用Spring?(how) Spring 框架是一个轻量级的框架,其...

  • 一篇文章让你了解基于Spring的测试

    针对Spring某个类及方法的单元测试,结合单元测试框架(比如JUnit)和Mock的框架(比如EasyMock)...

网友评论

      本文标题:Spring框架如何测试

      本文链接:https://www.haomeiwen.com/subject/rskzzqtx.html