zoukankan      html  css  js  c++  java
  • Struts2+Spring的UnitTest编写(使用StrutsTestCase的子类StrutsSpringTestCase)

           我们都知道struts2有自己的对象工厂即obejectFactory,但是你也可以使用spring来作为对象工厂,继承了spring之后的单元测试要加什么东西呢?
          答案是要加入下面的这些包:spring-web-2.5.6.jar,spring-context-2.5.6.jar,spring- beans-2.5.6.jar,struts2-spring-plugin-2.1.8.1.jar这四个包,在web.xml文件中还要加入如下的 代码行:
    Xml代码  收藏代码
    1.          <context-param>  
    2. <param-name>contextConfigLocation</param-name>  
    3. <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value>  
    4. </context-param>  
    5. <listener>  
    6. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    7. </listener>  

         如果你要编写测试类,要继承StrutsSpringTestCase,一定要这样做,我的测试类如下:

    Java代码  收藏代码
    1. import org.apache.struts2.StrutsSpringTestCase;  
    2. import org.apache.struts2.dispatcher.mapper.ActionMapping;  
    3. import com.opensymphony.xwork2.Action;  
    4. import com.opensymphony.xwork2.ActionProxy;  
    5.   
    6. public class HelloWorldSpringTest extends StrutsSpringTestCase {  
    7.   
    8. // @Override  
    9. // public String getContextLocations() {  
    10. // return "edu/ku/it/si/tutorial/action/TestAccountAction-context.xml";  
    11. // }  
    12. public void testGetActionMapping() throws Exception {  
    13. ActionMapping mapping = getActionMapping("/begin/helloWorld.action");  
    14. assertNotNull(mapping);  
    15. assertEquals("/begin", mapping.getNamespace());  
    16. assertEquals("helloWorld", mapping.getName());  
    17. }  
    18.   
    19. public void testGetActionProxy() throws Exception {  
    20. request.setParameter("username""FD");  
    21. ActionProxy proxy = getActionProxy("/begin/helloWorld.action");  
    22. assertNotNull(proxy);  
    23.   
    24. HelloWorld action = (HelloWorld) proxy.getAction();  
    25. assertNotNull(action);  
    26.   
    27. String result = proxy.execute();  
    28. assertEquals(Action.SUCCESS, result);  
    29. assertEquals("FD", action.getUsername());  
    30. }  
    31. }  

    测试类的内容和上一篇一样,只是继承类不一样,StrutsSpringTestCase是StrutsTestCase的子类,这个单元测试默 认读取配置文件applicationContext.xml的位置是类路径的根目录,如果你把这个文件放在不同位置或者取了一个不同的名称可以通过覆盖 父类中的protected java.lang.String getContextLocations()来指定你的配置文件。


    其他的地方和之前那篇日志中的一样,但是之前写的那个没有和spring继承的单元测试就不能使用了,如果你运行会报下面的错误:
    SEVERE:   [19:33.501] ********** FATAL ERROR STARTING UP STRUTS-SPRING INTEGRATION **********
    Looks like the Spring listener was not configured for your web app!
    Nothing will work until WebApplicationContextUtils returns a valid ApplicationContext.
    You might need to add the following to web.xml:
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
    SEVERE:   [19:33.541] Dispatcher initialization failed

    正如你所见到的我们已经指定了监听器,解决办法是吧你的测试类的父类改成StrutsSpringTestCase就可以解决问题。

    原文完,详细见:http://kang36897.blog.163.com/blog/static/170473732010710101238126/
    ----------------------------------

    后记:

    网上在struts2+Spring编写单元测试的问题里,发现大多问题是spring的配置文件applicationContext.xml 的路径或者多个时如何修改。其实很不明白为什么不在 applicationContext.xml主文件内倒入其他配置文件?这样web.xml不用配多个并且很清晰,而修改名字连 applicationContext.xml都不存在的话我觉得是自己找事做罢了。

    Xml代码  收藏代码
    1. <import resource="classpath:spring-datasource.xml" />  
    2.     <import resource="classpath:spring-dao.xml" />  
    3.     <import resource="classpath:spring-business.xml" />  
    4.     <import resource="classpath:spring-struts.xml" />  
  • 相关阅读:
    针对动态加载方式的C/C++动态链接库编写
    Delphi无法正确动态调用C++ dll库的几个原因
    Windows下VC++显示UTF-8编码中文
    小型C/C++项目的makefile编写
    树状树组(Binary Indexed Tree (BIT))的C++部分实现
    PLSQL创建Oracle定时任务
    Oracle:trunc()函数简介 时间处理及数字小数位处理
    Chrome inspect学习(四)本地环境/测试环境前端与客户端交互,涉及客户端代码报错,如何调试
    Chrome inspect学习(三)如何查看本地环境/测试环境下移动端内嵌H5页面在手机中真实渲染的DOM结构、CSS样式、接口调用
    Chrome inspect学习(二)如何查看线上环境移动端内嵌H5页面在手机中真实渲染的DOM结构、CSS样式、接口调用
  • 原文地址:https://www.cnblogs.com/lexus/p/2340587.html
Copyright © 2011-2022 走看看