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]