前言: 该代码适用于与spring框架整合的项目 代码: dao层的junit测试父类 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); } } service层的junit测试父类 junitServiceBase.java package cn.firstflag.crm.service; import org.apache.log4j.Logger; import org.junit.Before; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.test.context.ContextConfiguration; import cn.firstflag.crm.controller.BaseController; /* * @ClassName Service测试类父类 * @Author zhanmin.zheng * @CreateDate 2016/02/16 * @Version 1.0 */ public class JunitServiceBase { protected Logger log = Logger.getLogger(BaseController.class);//log4j protected ApplicationContext context;//应用上下文 public JunitServiceBase() { context = new ClassPathXmlApplicationContext(new String[]{ "classpath:root-context.xml","classpath:mvc-context.xml"}); } } controller层的junit测试父类 需要在spring配置中注入两个bean <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/> 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; } }