junit4+spring3.0.4.RELEASE测试单元基本实现

和之前的测试单元类类似,需要注意的是在要使用相对应的spring-test-3.0.0.RELEASE.jar

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations={"classpath:applicationContext-*.xml"})
@TransactionConfiguration(defaultRollback = false)
public class BaseTest  extends AbstractTransactionalJUnit4SpringContextTests{

    private static final String LOGIN_ID ="5552";
    private static final String PASS_WD ="555";
    @Autowired
    private LoginService loginService;
    
    public String httpLogin() throws BaseException{
        MockHttpServletRequest request = new MockHttpServletRequest();
        MockHttpServletResponse response = new MockHttpServletResponse();
        request.setMethod("POST");
        request.addParameter("loginid", LOGIN_ID);
        request.addParameter("passwd", PASS_WD);
        String loginResult = loginService.login(request, response);
        return loginResult;
    }
}

参数不过多解释了,文档参考:http://static.springsource.org/spring/docs/3.0.4.RELEASE/reference/html/testing.html#mock-objects

-----------------补充分割线--------2013-05-30----------------------------

  由于代码改造,很多地方使用了ActionContext,所以在Test中需要构造一个ActionContext。在Struts2.3.14的文档中有详细介绍Junit的结合,以下是我自身需求时,构造的ActionContext。

 ActionContext.setContext(new ActionContext(new HashMap<String, Object>()));
 ActionContext.getContext().setSession(new HashMap<String, Object>());

完整测试Demo如下:

import java.util.HashMap;

import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.kfseed.app.login.service.LoginService;
import com.kfseed.core.exception.BaseException;
import com.opensymphony.xwork2.ActionContext;

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations={"classpath:applicationContext-*.xml","classpath:abstractSessionTest.xml"})
@TransactionConfiguration(defaultRollback = false)
public class BaseTest  extends AbstractTransactionalJUnit4SpringContextTests{

    private static final String LOGIN_ID ="555";
    private static final String PASS_WD ="555";
    @Autowired
    private LoginService loginService;
    
    public String httpLogin() throws BaseException{
        startSession();
        startRequest();
        request.setMethod("POST");
        request.addParameter("loginid", LOGIN_ID);
        request.addParameter("passwd", PASS_WD);
        ActionContext.setContext(new ActionContext(new HashMap<String, Object>()));
        ActionContext.getContext().setSession(new HashMap<String, Object>());
        String loginResult = loginService.login(request, response);
        return loginResult;
    }
    
    
    protected MockHttpSession session;
    protected MockHttpServletRequest request;
    protected MockHttpServletResponse response;

    protected void startSession() {
        session = new MockHttpSession();
    }

    protected void endSession() {
        session.clearAttributes();
        session = null;
    }
    
    protected void startRequest() {
        request = new MockHttpServletRequest();
        response = new MockHttpServletResponse();
        request.setSession(session);
        RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
    }

    protected void endRequest() {
        ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).requestCompleted();
        RequestContextHolder.resetRequestAttributes();
        request = null;
    }
}

基础配置文件不说了,其中abstractSessionTest.xml内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 
    http://www.springframework.org/schema/jee 
    http://www.springframework.org/schema/jee/spring-jee-2.5.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    
    
    <bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
        <property name="scopes">
            <map>
                <entry key="session">
                    <bean class="org.springframework.web.context.request.SessionScope" />
                </entry>
                <entry key="request">
                    <bean class="org.springframework.web.context.request.RequestScope" />
                </entry>
            </map>
        </property>
    </bean>
</beans>

---------------------------------补充记录结束20130530-----------------------------------------

原文地址:https://www.cnblogs.com/GYoungBean/p/3037880.html