Spring MVC 单元测试


package cn.creditease.fso.news.controller;

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.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;

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.transaction.TransactionConfiguration;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext;

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = {"classpath:/spring-mvc-test.xml", "classpath:/spring-config.xml"})
@TransactionConfiguration(defaultRollback = true)
@Transactional
public class BaseSpringTestCase {

@Autowired
protected WebApplicationContext wac;

protected MockMvc mockMvc;

@Before
public void setUp() throws Exception {
this.mockMvc = webAppContextSetup(this.wac).build();
}

}




package cn.creditease.fso.news.controller;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;

import java.util.Map;

import junit.framework.TestCase;

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.MockHttpServletResponse;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext;

import com.alibaba.fastjson.JSON;

public class UserControllerTest extends BaseSpringTestCase{

private static int userId = -1;

@Test
public void testGetUsersPage() throws Exception {
mockMvc.perform((get("/user/getUsersPage.do")))
.andExpect(status().isOk())
.andExpect(forwardedUrl("/WEB-INF/views/index.jsp"));
}

@Test
public void testAddUser() throws Exception {
MockHttpServletResponse response = mockMvc.perform((post("/user/addUser.do")
.param("email", "test@qq.com")
.param("password", "test")
.param("nickname", "test")
.param("cellphone", "12345678901")))
.andExpect(status().isOk())
.andExpect(jsonPath("$.respCode").value(0)).andReturn().getResponse();
System.out.println();
userId = (Integer)JSON.parseObject(response.getContentAsString(), Map.class).get("userId");
}

@Test
public void testGetUsers() throws Exception {
testAddUser();
mockMvc.perform((get("/user/getUsers.do")
.param("search", "test@qq.com")
.param("sort", "")
.param("order", "")
.param("limit", "10")
.param("offset", "0")))
.andExpect(status().isOk())
.andExpect(jsonPath("$.rows[0].email").value("test@qq.com"));
}


@Test
public void testUpdateUser() throws Exception {
testAddUser();
mockMvc.perform((post("/user/addUser.do")
.param("userId", userId+"")
.param("email", "test@qq.com1")
.param("password", "test1")
.param("nickname", "test1")
.param("cellphone", "123456789011")))
.andExpect(status().isOk())
.andExpect(jsonPath("$.respCode").value(0));
}

@Test
public void testGetUserRoles() throws Exception {
testAddUser();
mockMvc.perform((get("/user/getUserRoles.do")
.param("userId", userId+"")))
.andExpect(status().isOk())
.andExpect(jsonPath("$.respCode").value(0));
}

@Test
public void testEditUserRoles() throws Exception {
testAddUser();
mockMvc.perform((post("/user/editUserRoles.do")
.param("roleIds", "1,2,3")
.param("userId", userId+"")))
.andExpect(status().isOk())
.andExpect(jsonPath("$.respCode").value(0));
}

@Test
public void testDeleteUser() throws Exception {
testAddUser();
mockMvc.perform((post("/user/deleteUser.do")
.param("userId", userId+"")))
.andExpect(status().isOk())
.andExpect(jsonPath("$.respCode").value(0));
}

}




<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.2.12.RELEASE</version>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>0.8.1</version>
</dependency>



[quote]单元测试的一个要求就是每个测试独立运行,不然怎么叫单元测试呢?单元测试的目的就是测试最小单位的正确性,隔离和其他部分的关联,自然也不能有依赖,不然,一定测试通不过,你无法知道是单元内部的问题,还是外部环境的问题 引自:http://www.iteye.com/problems/69843[/quote]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值