【问题标题】:Struts2 + Spring3 + JUnit4 IntegrationStruts2 + Spring3 + JUnit4 集成
【发布时间】:2013-10-31 09:21:33
【问题描述】:

我尝试为 Struts2 和 Spring3 集成编写单元测试。 我使用 struts2-junit 插件进行 jUnit4 测试。 但问题是动作代理返回 null。

我该如何解决?

下面的代码是我的测试类和其他配置。 测试类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:applicationContext.xml" })
public class StrutsTest extends StrutsSpringJUnit4TestCase<MyAction> {
    @Test
    public void myTest() throws Exception {
        ActionProxy proxy = getActionProxy("/myaction");
        MyAction myAction = (MyAction) proxy.getAction();
        request.setParameter("personBean.firstName", "Joe");
        request.setParameter("personBean.lastName", "Doe");
        String result = myAction.execute();
        assertEquals("Error", ActionSupport.SUCCESS, result);
    }
}

MyAction.java:

@Component
@Scope("prototype")
public class MyAction implements Action  {
    private Person personBean;

    public String execute() throws Exception {
        System.out.println("MyAction is called!");
        System.out.println("personBean.firstName"+personBean.getFirstName());
        System.out.println("personBean.firstName"+personBean.getFirstName());
        return ActionSupport.ERROR;
    }

}

struts.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <!-- Constants -->
    <constant name="struts.i18n.encoding" value="UTF-8" />
    <constant name="struts.action.extension" value="do" />
    <constant name="struts.objectFactory" value="spring" />
    <constant name="struts.custom.i18n.resources" value="applicationResources, actionErrorMapping, smsMessages" />
    <constant name="struts.codebehind.pathPrefix" value="/WEB-INF/" />
    <constant name="struts.enable.SlashesInActionNames" value="true" />
    <constant name="struts.multipart.maxSize" value="2097152" />
    <constant name="struts.ui.theme" value="simple" />
    <constant name="struts.devMode" value="true" />

    <package name="default" namespace="/" extends="struts-default">
        <action name="myaction" class="com.struts.actions.MyAction">
            <result>/index.jsp</result>
        </action>
    </package>
</struts>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <display-name>Archetype Created Web Application</display-name>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>FORWARD</dispatcher>
        <dispatcher>REQUEST</dispatcher>
    </filter-mapping>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
        <listener-class>org.apache.struts2.tiles.StrutsTilesListener</listener-class>
    </listener>
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

applicationContext.xml:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
             http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">
    <context:annotation-config />
    <context:component-scan base-package="com"  />
</beans>

【问题讨论】:

    标签: java struts2 junit4 spring-3


    【解决方案1】:
    1. 确保您已包含 struts2-spring-plugin:

      <dependency>
          <groupId>org.apache.struts</groupId>
          <artifactId>struts2-spring-plugin</artifactId>
          <version>STRUTS_VERSION</version>
      </dependency>
      
    2. 在 web.xml 中尝试使用

      <param-value>classpath*:applicationContext.xml</param-value>
      
    3. 在struts.xml中尝试使用

      <constant name="struts.objectFactory" 
                value="org.apache.struts2.spring.StrutsSpringObjectFactory" />
      

    第一次尝试在我脑海中闪过……希望对你有所帮助。

    【讨论】:

    • 谢谢。他们都已经完成了。弹簧工作正常。问题是 ActionProxy 返回 null。我在调试中查看了它,似乎我的 struts.xml 配置已读取但没有产生我的操作。有什么问题?
    • 这真的很奇怪...我通过编辑 web.xml、applicationContext.xml、struts.xml 等进行了多种尝试...但在我的测试中我总是找到代理:|你确定你的文件夹被正确打包,库都是相同的版本,等等?
    • 我将我的项目上传到了 Dropbox。您可以查看:dropbox.com/sh/chy0hp6at74fytx/VQ0eXKEUN6
    • 顺便说一句,web.xml 在 WEB-INF 文件夹下,applicationContext.xml 和 struts.xml 在 src/main/resources 下
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-25
    相关资源
    最近更新 更多