springMvc controller 单元测试
程序员文章站
2022-04-29 16:38:07
...
springMvc 3.1.2版本 controller 单元测试
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/WEB-INF/monitor-servlet.xml", "classpath:/spring/spring-core.xml"})
public class JobControllerTest {
@Autowired
public RequestMappingHandlerAdapter handlerAdapter;
@Autowired
private JobController jobController;
private static MockHttpServletRequest request;
private static MockHttpServletResponse response;
@BeforeClass
public static void before() {
request = new MockHttpServletRequest();
request.setCharacterEncoding("UTF-8");
response = new MockHttpServletResponse();
}
@Test
public void testList() {
request.setRequestURI("/job/list");
request.setMethod(HttpMethod.POST.name());
ModelAndView mv = null;
try {
mv = handlerAdapter.handle(request, response, new HandlerMethod(jobController, "list"));
} catch (Exception e) {
e.printStackTrace();
}
Assert.assertNotNull(mv);
Assert.assertEquals(response.getStatus(), 200);
Assert.assertEquals(mv.getViewName(), "/job/job_list");
}
}
转载于:https://my.oschina.net/u/569410/blog/89764
推荐阅读
-
Zend Framework教程之动作的基类Zend_Controller_Action详解
-
Zend Framework教程之分发器Zend_Controller_Dispatcher用法详解
-
Zend Framework动作助手(Zend_Controller_Action_Helper)用法详解
-
Springmvc 4.x利用@ResponseBody返回Json数据的方法
-
SpringMVC使用MultipartFile实现文件上传
-
springmvc限流拦截器的示例代码
-
SpringMVC 限流的示例代码
-
解决SpringMvc后台接收json数据中文乱码问题的几种方法
-
详解AngularJs与SpringMVC简单结合使用
-
SpringMVC 方法四种类型返回值总结(你用过几种)