zoukankan      html  css  js  c++  java
  • 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-----------------------------------------

  • 相关阅读:
    android adb
    5 个免费的受欢迎的 SQLite 管理工具
    [Android]通过setImageURI设置网络上面的图片
    Android TextView实现长按复制文本功能的方法
    View工作原理(四)view的layout过程
    Anaroid WebView详解大全
    Android 如何在Eclipse中查看Android API源码以及support包源码
    关于Android的.so文件你所需要知道的
    AS问题解决系列3—iCCP: Not recognizing known sRGB profile(转)
    安卓App设计博文
  • 原文地址:https://www.cnblogs.com/GYoungBean/p/3037880.html
Copyright © 2011-2022 走看看