spring 3.1+ 单元测试

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()
	{
		
	}
}


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值