码迷,mamicode.com
首页 > 编程语言 > 详细

spring框架中使用junt单元测试

时间:2016-02-26 23:39:49      阅读:379      评论:0      收藏:0      [点我收藏+]

标签:

前言:

该代码适用于与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单元测试

标签:

原文地址:http://www.cnblogs.com/sz-zzm/p/5222069.html

(0)
(0)
   
举报
评论 一句话评论(0
0条  
登录后才能评论!
© 2014 mamicode.com 版权所有 京ICP备13008772号-2
迷上了代码!