软件测试

收藏 / 订阅

Spring框架中使用Junit单元测试__软件测试

  前言:

  该代码适用于与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;

}

}

喜欢 (88) or分享 (0)
首页  上一页  12345678910  下一页 尾页 共73条记录 9/10

网友评论:7

  1. 小编 1年前 (2015-03-22) #5

    软件测试入门 :mrgreen:

留言主题 *

您的姓名 *

电子信箱:

电话号码:

请你留言: