spring mvc 使用junit 测试 controller
来源:互联网 发布:c语言编译器哪个好2016 编辑:程序博客网 时间:2024/06/06 01:06
直接上java代码:
package com.line.up.controller;import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;import java.util.Date;import org.apache.log4j.Logger;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.web.WebAppConfiguration;import org.springframework.test.web.servlet.MockMvc;import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;import org.springframework.test.web.servlet.result.MockMvcResultHandlers;import org.springframework.test.web.servlet.result.MockMvcResultMatchers;import org.springframework.util.LinkedMultiValueMap;import org.springframework.util.MultiValueMap;import org.springframework.web.context.WebApplicationContext;import com.line.up.dto.feedback.FeedbackDto;@RunWith(SpringJUnit4ClassRunner.class)@WebAppConfiguration@ContextConfiguration({"classpath:spring-servlet.xml"})public class FeedbackControllerTest {private static Logger LOG = Logger.getLogger(FeedbackControllerTest.class);private MockMvc mockMvc;@Autowiredprivate WebApplicationContext webApplicatoinContext;@Testpublic void testFeedback(){LOG.info(new Date()+"xxxxxxxxxxxxxxxxx");LOG.info("account insert...");try {FeedbackDto feedbackDto = new FeedbackDto();feedbackDto.setUserName("zzzz");feedbackDto.setCreatedTime(new Date());MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();params.add("userName", "test1");params.add("userMobile", "zzzzz");params.add("userEmail", "test1@163.com");params.add("feedbackContent", "fdasgasfdasfdas固定撒发达");this.mockMvc = webAppContextSetup(this.webApplicatoinContext).build();this.mockMvc.perform((MockMvcRequestBuilders.post("/feedback/create.do").params(params))).andExpect(MockMvcResultMatchers.status().isOk()).andDo(MockMvcResultHandlers.print());} catch (Exception e) {LOG.error(e.getMessage(), e);}}}
@RequestMapping(value="/create.do", method = RequestMethod.POST)public String create(HttpServletRequest request){LOG.debug("反馈。。。");FeedbackDto feedbackDto = new FeedbackDto();//这里参数需要用request来接收feedbackDto.setUserName(request.getParameter("userName"));feedbackDto.setFeedbackSource(0);feedbackDto.setDeleted(0);feedbackDto.setVersiond(0);feedbackDto.setCreatedTime(new Date());try {LOG.info("feedbackDtoStr:"+JSONObject.toJSONString(feedbackDto));feedbackBiz.create(feedbackDto);LOG.debug("result:true");} catch (LineException e) {LOG.error(e.getMessage(),e);}return "register";}输出调用结果:
MockHttpServletRequest: HTTP Method = POST Request URI = /feedback/create.do Parameters = {userName=[test1], userMobile=[zzzzz], userEmail=[test1@163.com], feedbackContent=[fdasgasfdasfdas固定撒发达]} Headers = {}Handler: Type = com.line.up.controller.FeedbackController Method = public java.lang.String com.line.up.controller.FeedbackController.create(javax.servlet.http.HttpServletRequest)Async: Async started = false Async result = nullResolved Exception: Type = nullModelAndView: View name = register View = null Model = nullFlashMap: Attributes = nullMockHttpServletResponse: Status = 200 Error message = null Headers = {} Content type = null Body = Forwarded URL = /WEB-INF/html/register.html Redirected URL = null Cookies = []
0 0
- spring mvc 使用junit 测试 controller
- 使用MockMvc测试Spring mvc Controller
- 使用MockMvc测试Spring mvc Controller
- 使用MockMvc测试Spring mvc Controller
- Spring Boot Junit 测试Controller
- spring mvc测试controller
- Spring MVC对Controller实现Junit Case
- 使用Mock MVC来进行Controller 层的junit的测试
- Junit测试Controller(MockMVC使用)
- Junit测试Controller(MockMVC使用)
- Spring MVC参数化测试 - Junit Parameterized
- Junit测试controller
- 使用junit进行Spring测试
- Spring Controller Junit例子
- Spring MVC(三)基于上一篇使用JUnit编写测试用例
- Controller 使用RestTemplate进行单元测试 spring mvc
- spring mvc中controller使用介绍
- spring mvc + junit4测试(包含测试controller)
- 素数链表——初级素数表+链表基本操作
- 快速排序Quicksort(附快速排序程序)
- 20170505Windows02_WindowsDataType
- 【Android】ContentProvider详解(一)
- 生成KD树并用KD树求最近邻点
- spring mvc 使用junit 测试 controller
- HTDP学习笔记(1)--DrRacket的安装和配置
- Android 通知栏Notification的整合 全面学习 (一个DEMO让你完全了解它)
- ul与ol标签,链接
- Java并发编程示例(二):获取和设置线程信息
- 13. Roman to Integer
- hdu 1796 容斥原理
- C++智能指针原理分析与简单实现
- [转]Ubuntu 16.04LTS 安装 MATLAB 2014B