2013-10-22 55 views
1

我試圖爲Struts2和Spring3集成編寫單元測試。 我已經使用jUnit4測試的struts2-junit插件。 但事情是行動代理返回null。Struts2 + Spring3 + JUnit4集成

我該如何解決?

在下面的代碼是我的測試類和其他配置。 測試類

@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> 

回答

0
  1. 確定您已經列入Struts2的彈簧,插件:

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

第一次嘗試過我的想法...希望有幫助。

+0

謝謝。他們都已經完成了。春天工作正常。事情是ActionProxy返回null。我在調試時查了一下,似乎我的struts.xml配置被讀取,但並沒有產生我的操作。什麼可能是錯誤的? – yyy

+0

您是否在使用['struts2-junit-plugin'](http://mvnrepository.com/artifact/org.apache.struts/struts2-junit-plugin)或['struts2-junit4-plugin'](http:/ /mvnrepository.com/artifact/com.googlecode.struts2-conversation/struts2-junit4-plugin)? –

+0

struts2-junit-plugin。它有junit4庫。 – yyy