Spring框架如何测试

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()));
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容