Struts2 + Spring3 + JUnit4集成

时间:2013-10-22 13:16:32

标签: java struts2 junit4 spring-3

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

1 个答案:

答案 0 :(得分: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" />
    
  4. 首先尝试穿越我的脑海......希望有所帮助。

相关问题