springmvc controller junit 测试
第一次搭建SSM框架,整合SpringMVC完成后进行Controller测试,找资料并解决问题。
下图是我的完整目录:
1 建立UserController类
代码清单 1-1:UserController.java
package org.sei.controller;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.sei.pojo.User;
import org.sei.service.UserService;
@Controller
@RequestMapping("/user")
public class UserController
{
@Resource
private UserService userService;
@RequestMapping("/showUser1")
public String toIndex(HttpServletRequest request,Model model)
{
int userId = Integer.parseInt(request.getParameter("id"));
User user = this.userService.getUserById(userId);
model.addAttribute("user", user);
return user.getUserName();
}
@RequestMapping("/showUser3")
public String toIndex1(User u,Model model) //注意:参数不同
{
int userId = u.getId();
User user = this.userService.getUserById(userId);
model.addAttribute("user", user);
return user.getUserName();
}
}
2 建立测试类
代码清单 2-1:MockTestController.java
package org.sei.testmybatis;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
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 static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:spring-mybatis.xml","classpath:spring-mvc.xml"})
@WebAppConfiguration
public class MockTestController
{
protected MockMvc mockMvc;
@Autowired
protected WebApplicationContext wac;
@Before() //这个方法在每个方法执行之前都会执行一遍
public void setup()
{
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build(); //初始化MockMvc对象
}
@Test
public void getAllCategoryTest() throws Exception
{
String responseString = mockMvc.perform
(
get("/user/showUser1") //请求的url,请求的方法是get
//get("/user/showUser2") //请求的url,请求的方法是get
.contentType(MediaType.APPLICATION_FORM_URLENCODED)//数据的格式
.param("id","1") //添加参数(可以添加多个)
//.param("id","3") //添加参数(可以添加多个)
)
.andExpect(status().isOk()) //返回的状态是200
.andDo(print()) //打印出请求和相应的内容
.andReturn().getResponse().getContentAsString(); //将相应的数据转换为字符串
System.out.println("-----返回的json = " + responseString);
}
}
3 JUnit测试
测试的数据库表如下:
测试1:参数(HttpServletRequest request,Model model)
测试2:参数(User u,Model model)
4 几个jar包
很多问题都来自于确少jar包。
下面给出几个jar包的Maven依赖:
代码清单 4-1:pom.xml (局部)
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<!-- 映入JSON -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.8.3</version>
</dependency>
参考:
1. Junit测试Controller(MockMVC使用),传输@RequestBody数据解决办法
2. 基于Spring MVC做单元测试(一)——使用Spring Test框架
3. junit 测试 springmvc controller
4. spring-mvc junit test