3.1单元测试
package test.com.sharp.freemarker;
/**===========================================
* Copyright (C) 2013 Tempus
* All rights reserved
*
* 项 目 名: TMC
* 文 件 名:TestUserService.java
* 版本信息: V1.0.0
* 作 者: peng.xiao
* 日 期: 2013-1-17-下午01:39:43
*
============================================*/
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
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.annotation.Rollback;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import com.sharp.freemarker.basedata.controller.UserController;
import com.sharp.freemarker.basedata.service.IUserService;
public class TestUserService extends BaseUnit
{
@Resource(name="userService")
private IUserService userService;
@Autowired
private UserController userController;
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
//3.1+ handle
@Autowired
private RequestMappingHandlerAdapter handlerAdapter;
// @Autowired 3.0 handle
// private AnnotationMethodHandlerAdapter handlerAdapter;
@Before
public void init()
{
}
@After
public void destory()
{
}
@Test
@Transactional
@Rollback(false)
public void testSave() throws Exception
{
request.addParameter("name", "sharp");
request.addParameter("pass", "123456");
request.setRequestURI("/basedata/login");
request.setMethod(HttpMethod.POST.name());
ModelAndView view = handlerAdapter.handle(request, response, new HandlerMethod(userController,"login",String.class, String.class));
Assert.assertEquals("welcome", view.getViewName());
}
@Test
@Transactional
@Rollback(false)
public void testDelete()
{
}
@Test
@Transactional
@Rollback(false)
public void testUpdate()
{
}
@Test
public void testget()
{
}
}