1

我曾尝试为 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>

网页.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>

应用程序上下文.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>
4

1 回答 1

0
  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" />
    

首先尝试在我脑海中浮现……希望对您有所帮助。

于 2013-10-22T14:18:10.433 回答