SpringMVC单元测试之MockMVC,模拟登入用户
发布时间:2021-07-06 05:26:00 所属栏目:大数据 来源: https://www.cnblogs.com/xiado
导读:今天介绍一下springMVC的单元测试,可以参考spring官方文档进行前提准备,springmvc的demo工程,这里就不做叙述了pom.xml[html] view plain copy 在CODE上查看代码片派生到我的代码片 dependency groupIdorg.springframework/groupId artifactIdspring-core/
今天介绍一下springMVC的单元测试,可以参考spring官方文档进行 前提准备,springmvc的demo工程,这里就不做叙述了 pom.xml [html] view plain copy 在CODE上查看代码片派生到我的代码片 <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> controller层 [java] view plain copy 在CODE上查看代码片派生到我的代码片 package controller; import javax.servlet.http.HttpSession; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import service.UserService; import domain.User; /** * UserController. * @author Leon Lee */ @RestController @RequestMapping(value = "user") public class UserController { * * UserService interface. @Autowired private UserService userService; * * Get user MSG. * @param userId * @return user Msg @RequestMapping(value = userMsg/{userId}",method = RequestMethod.GET) public User getUserMsg(@PathVariable(value = userId) String userId) { return userService.getUserMsg(userId); } * * Update user MSG. * @param userId * @param userName * @return updated user MSG RequestMethod.PUT) public User putUserMsg(@PathVariable(value = ) String userId,@RequestParam String userName,HttpSession session){ if(null == (String)session.getAttribute(loginUser)) return new User(); System.out.println((String)session.getAttribute()); userService.putUserMsg(userId,userName); } * * Delete user. * @param userId * @return deleted user MSG RequestMethod.DELETE) public User delUserMsg(@PathVariable(value = ) String userId){ userService.delUserMsg(userId); } * * Add user. * @param userName * @return added user MSG userMsg RequestMethod.POST) public User postUserMsg(@RequestParam String userName){ userService.postUserMsg(userName); } * * login User. Note that do not send password as url. * @param userId * @param password * @return userMsg/{userId}/{password} boolean loginUser(@PathVariable String userId,@PathVariable String password,1)">if(".equals(userId)&&.equals(password)){ session.setAttribute(,userId); true; } false; } } 单元测试类 这里的静态导入比较重要,有时候没办法自动导入的 就是下面的 import static xxx.* 另一点, [java] view plain copy 在CODE上查看代码片派生到我的代码片 @ContextConfiguration(locations = {classpath:applicationContext.xmlclasspath:applicationContext.mvc.xml}) 代表的是加载的配置文件,可以根据需要进行添加 [java] view plain copy 在CODE上查看代码片派生到我的代码片 package controller.test; import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; import javax.servlet.http.HttpSession; 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.mock.web.MockHttpSession; 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.test.web.servlet.MvcResult; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.context.WebApplicationContext; * * spring mvc Test. * @author Leon Lee * @since spring-4.1.7 */ // spring 4.3 change to SpringRunner.class @RunWith(SpringJUnit4ClassRunner.) @WebAppConfiguration @ContextConfiguration(locations = {}) do rollback @TransactionConfiguration(defaultRollback = ) @Transactional TestTemplate { @Autowired WebApplicationContext wac; MockMvc mockMvc; MockHttpSession session; @Before void setup() { init applicationContext this.mockMvc = webAppContextSetup(this.wac).build(); this.session = MockHttpSession(); } @Test getUserMsg() throws Exception { get using get .mockMvc .perform((get(/user/userMsg/003)) .accept(MediaType.parseMediaType(application/json;charset=UTF-8))) .andExpect(status().isOk()) .andExpect(content().contentType()) .andDo(print()); print } @Test don't rollback @Rollback() putUserMsg() throws Exception { update using put .mockMvc .perform((put()) .contentType(MediaType.APPLICATION_FORM_URLENCODED) .param(userName新名字03号) .session((MockHttpSession)getLoginSession()) .accept(MediaType.parseMediaType()) ) .andExpect(status().isOk()) .andExpect(content().contentType( delUser() throws Exception { delete using delete .mockMvc .perform((delete(/user/userMsg/004print } @Test postUser() throws Exception{ add using post .mockMvc .perform((post(/user/userMsg)) .param(最新的用户) .accept(MediaType.parseMediaType( } * * 获取登入信息session * @return * @throws Exception */ HttpSession getLoginSession() throws Exception{ mock request get login session url = /xxx/xxx/{username}/{password} MvcResult result = .mockMvc .perform((/user/userMsg/loginUser/loginUser))) .andExpect(status().isOk()) .andReturn(); result.getRequest().getSession(); } } ? (编辑:北几岛) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |