我是靠谱客的博主 玩命超短裙,最近开发中收集的这篇文章主要介绍spring-mvc-test(单元测试),觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

一、spring-mvc单元测试依赖的相关包


<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>

二、初始化容器

@ContextConfiguration(locations = {"classpath:application-context.xml","classpath:dispatcher-servlet.xml","classpath:proxy-context.xml"})
@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)

三、初始化测试单元

</pre><pre name="code" class="java">
@Autowired
private WebApplicationContext webContext;
private MockMvc mockMvc;
@Before
public void setUp(){
mockMvc = MockMvcBuilders.webAppContextSetup(webContext).build();
}
四、单元测试-结合了protobuf的使用

 @Test
public void testLocalLogin() throws Exception{
ResultActions resultActions = mockMvc.perform(MockMvcRequestBuilders.get("/qm/localServer.action")
.param("key", "qmUserLogin")
.param("loginType", "1")
.param("locationId", "022")
.param("pname", "pname1")
.param("userId", "372993"));
MvcResult mvcResult = resultActions.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(MockMvcResultHandlers.print()).andReturn();
TreeFrogMsg.UserLoginResponse userLoginResponse = TreeFrogMsg.UserLoginResponse.parseFrom(mvcResult.getResponse()
.getContentAsByteArray());
System.out.println(userLoginResponse.getResultCode());
System.out.println(userLoginResponse.getWebKey());
System.out.println(userLoginResponse.getUserBaseInfo().getNickName());
}

五、完整的测试代码

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
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.MvcResult;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
/**
* Created by sz0816 on 14-5-12.
*/
@ContextConfiguration(locations = {"classpath:application-context.xml","classpath:dispatcher-servlet.xml","classpath:proxy-context.xml"})
@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class UserLoginTest {
@Autowired
private WebApplicationContext webContext;
private MockMvc mockMvc;
@Before
public void setUp(){
mockMvc = MockMvcBuilders.webAppContextSetup(webContext).build();
}
@Test
public void testLocalLogin() throws Exception{
ResultActions resultActions = mockMvc.perform(MockMvcRequestBuilders.get("/qm/localServer.action")
.param("key", "qmUserLogin")
.param("loginType", "1")
.param("pname", "pname1")
.param("userId", "372993"));
MvcResult mvcResult = resultActions.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(MockMvcResultHandlers.print()).andReturn();
TreeFrogMsg.UserLoginResponse userLoginResponse = TreeFrogMsg.UserLoginResponse.parseFrom(mvcResult.getResponse()
.getContentAsByteArray());
System.out.println(userLoginResponse.getResultCode());
System.out.println(userLoginResponse.getWebKey());
System.out.println(userLoginResponse.getUserBaseInfo().getNickName());
}
}


最后

以上就是玩命超短裙为你收集整理的spring-mvc-test(单元测试)的全部内容,希望文章能够帮你解决spring-mvc-test(单元测试)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(53)

评论列表共有 0 条评论

立即
投稿
返回
顶部