您的位置:軟件測(cè)試 > 開(kāi)源軟件測(cè)試 > 開(kāi)源單元測(cè)試工具 > junit
Spring框架中使用Junit單元測(cè)試
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時(shí)間:[ 2016/3/2 13:37:39 ] 推薦標(biāo)簽:單元測(cè)試 Junit

  前言:
  該代碼適用于與spring框架整合的項(xiàng)目
  代碼:
  dao層的junit測(cè)試父類
  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測(cè)試父類
  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測(cè)試類父類
* @Author zhanmin.zheng
* @CreateDate 2016/02/16
* @Version 1.0
*/
public class JunitServiceBase {
protected Logger log = Logger.getLogger(BaseController.class);//log4j
protected ApplicationContext context;//應(yīng)用上下文
public JunitServiceBase() {
context = new ClassPathXmlApplicationContext(new String[]{
"classpath:root-context.xml","classpath:mvc-context.xml"});
}
}
  controller層的junit測(cè)試父類
  需要在spring配置中注入兩個(gè)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;
}
}

軟件測(cè)試工具 | 聯(lián)系我們 | 投訴建議 | 誠(chéng)聘英才 | 申請(qǐng)使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd