spring框架中使用junt单元测试
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring框架中使用junt单元测试相关的知识,希望对你有一定的参考价值。
前言:
该代码适用于与spring框架整合的项目
代码:
dao层的junit测试父类,需要在spring配置中注入两个bean
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
junitDaoBase.java
package cn.firstflag.crm.dao; import javax.annotation.Resource; import javax.sql.DataSource; import org.apache.log4j.Logger; 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; import cn.firstflag.crm.controller.BaseController; /** * @Description junit DAO层父类 * @Author zhanmin.zheng * @CreateDate 2016/01/1 * @ModifyDate * @Version 1.0 */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:root-context.xml","classpath:mvc-context.xml"})//spring配置文件全部加入 public class JUnitDaoBase extends AbstractTransactionalJUnit4SpringContextTests { protected Logger log = Logger.getLogger(BaseController.class);//log4j @Override @Resource(name = "dataSource") public void setDataSource(DataSource dataSource) { super.setDataSource(dataSource); } }
controller层的junit测试父类
JUnitActionBase.java
package cn.firstflag.crm.controller; import static org.junit.Assert.*; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.junit.BeforeClass; import org.junit.Test; import org.springframework.mock.web.MockServletContext; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.XmlWebApplicationContext; import org.springframework.web.servlet.HandlerAdapter; import org.springframework.web.servlet.HandlerExecutionChain; import org.springframework.web.servlet.HandlerMapping; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter; import org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping; /** * @Description junit 控制层父类 * @Author zhanmin.zheng * @CreateDate 2016/01/1 * @ModifyDate * @Version 1.0 */ public class JUnitActionBase { private static HandlerMapping handlerMapping; private static HandlerAdapter handlerAdapter; @BeforeClass public static void setup() throws Exception{ if (handlerMapping == null) { String[] configs = { "classpath:root-context.xml", "classpath:mvc-context.xml" XmlWebApplicationContext context = new XmlWebApplicationContext(); context.setConfigLocations(configs); MockServletContext msc = new MockServletContext(); context.setServletContext(msc); context.refresh(); msc.setAttribute( WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, context);// TODO handlerMapping = (HandlerMapping) context .getBean(DefaultAnnotationHandlerMapping.class); handlerAdapter = (HandlerAdapter) context .getBean(context .getBeanNamesForType(AnnotationMethodHandlerAdapter.class)[0]); } } /** * * @param request * @param response * @return * @throws Exception */ public ModelAndView excuteAction(HttpServletRequest request, HttpServletResponse response) throws Exception { request.setAttribute(HandlerMapping.INTROSPECT_TYPE_LEVEL_MAPPING, true); HandlerExecutionChain chain = handlerMapping.getHandler(request); ModelAndView model = null; try { model = handlerAdapter .handle(request, response, chain.getHandler()); } catch (Exception e) { e.printStackTrace(); } return model; } }
以上是关于spring框架中使用junt单元测试的主要内容,如果未能解决你的问题,请参考以下文章