软件测试培训
达内IT学院
400-996-5531
SpringMVC controller测试较简单,从功能角度划分,可分为两种。一种是调用请求路径测试,另一种是直接调用Controller方法测试。
调用请求路径测试
通过请求路径调用,请求需要经过拦截器,再到对应的Controller方法处理
被测试代码示例
import com.agoura.agoura.entity.Members;
import com.agoura.agoura.service.MembersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
@Controller
@RequestMapping("/api")
public class MembersController {
@Autowired
private MembersService membersService;
@RequestMapping(value = "/user", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
@ResponseBody
public String getUser(HttpServletRequest request, @RequestBody String str,
@RequestParam(value = "userId") String userId) {
Members result = membersService.getMemberById(Integer.valueOf(userId));
return result.toString();
}
}
测试代码示例
import com.agoura.agoura.entity.Members;
import com.agoura.agoura.service.MembersService;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
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.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@RunWith(SpringJUnit4ClassRunner.class) //调用Spring单元测试类
@WebAppConfiguration //调用Java Web组件,如自动注入ServletContext Bean等
@ContextConfiguration(locations = {"classpath*:spring-*.xml"}) //加载Spring配置文件
public class MembersControllerTest2 {
@Autowired
protected WebApplicationContext wac;
@Mock
private MembersService service;
@InjectMocks
private MembersController controller; //需要测试的Controller
private MockMvc mockMvc; //SpringMVC提供的Controller测试类
private String url;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
mockMvc = MockMvcBuilders.standaloneSetup(wac).build();
}
@Test
public void testGetUser() throws Exception {
url = "the URL to Controller";
Members m = new Members(3, "wangwu", 1, 5, "12131232342");
when(service.getMemberById(Mockito.anyInt())).thenReturn(m);
MvcResult result = mockMvc
.perform(MockMvcRequestBuilders.post(url)
.accept(MediaType.APPLICATION_JSON)
.param("userId", "3"))
.andDo(print())
.andExpect(status().isOk())
.andExpect(model().attributeExists("name"))
.andReturn();
assertEquals(m.toString(), result.getModelAndView().getModel().get("user").toString());
}
}
调用Controller方法测试
无法对拦截器和URL路径配置进行测试。测试方法类似于service层单元测试,可以对依赖的service层进行mock。
被测试代码示例
测试代码同调用请求路径测试方式部分一样
测试代码示例
import com.agoura.agoura.entity.Members;
import com.agoura.agoura.service.MembersService;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
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.setup.MockMvcBuilders;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;
@RunWith(SpringJUnit4ClassRunner.class) //调用Spring单元测试类
@WebAppConfiguration //调用Java Web组件,如自动注入ServletContext Bean等
@ContextConfiguration(locations = {"classpath*:spring-*.xml"}) //加载Spring配置文件
public class MembersControllerTest {
@Mock
private MembersService service;
@InjectMocks
private MembersController controller; //需要测试的Controller
private MockHttpServletRequest request;
private MockMvc mockMvc; //SpringMVC提供的Controller测试类
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
request = new MockHttpServletRequest();
}
@Test
public void testGetUser() throws Exception {
request.addParameter("userId", "100100");
// request.addParameter("id", "101010"); //如果需要从request取数据,可以在request中添加parameter
Members m = new Members(3, "wangwu", 1, 5, "12131232342");
when(service.getMemberById(Mockito.anyInt())).thenReturn(m);
String response = new String();
String result = controller.getUser(request, response, "3");
assertEquals(m.toString(), result);
}
}
填写下面表单即可预约申请免费试听! 怕学不会?助教全程陪读,随时解惑!担心就业?一地学习,可全国推荐就业!
Copyright © 京ICP备08000853号-56 京公网安备 11010802029508号 达内时代科技集团有限公司 版权所有
Tedu.cn All Rights Reserved