转自:http://blog.csdn.net/yin_jw/article/details/24726941
分类: Spring开源框架2014-04-29 17:01 1786人阅读 评论(2) 收藏 举报
准备SpringMVC环境
注意:使用mock测试需要引入spring-test包
Base类:加载配置文件
[java] view plaincopy
- package com.wyy.snail.user.controller;
- import org.junit.runner.RunWith;
- import org.springframework.test.context.ContextConfiguration;
- import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
- import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
- @ContextConfiguration(locations = { "classpath:config/spring/spring-*.xml",
- "classpath:config/spring/service-mybatis.xml" })
- @RunWith(SpringJUnit4ClassRunner.class)
- public class BaseControllerTest extends AbstractTransactionalJUnit4SpringContextTests {
- }
测试类:具体业务
[java] view plaincopy
- package com.wyy.snail.user.controller;
- import java.io.IOException;
- import org.junit.Test;
- import org.springframework.mock.web.MockHttpServletRequest;
- import org.springframework.mock.web.MockHttpServletResponse;
- public class LoginControllerTest extends BaseControllerTest {
- @Test
- public void testLogin() throws IOException {
- LoginController loginController = (LoginController) this.applicationContext.getBean("loginController");
- MockHttpServletRequest request = new MockHttpServletRequest();
- MockHttpServletResponse response = new MockHttpServletResponse();
- request.setMethod("POST");
- request.addParameter("username", "aa");
- request.addParameter("password", "bb");
- loginController.login(request, response, "", "", "");
- }
时间: 2024-10-26 06:02:45