1

我正在为我的应用程序使用 Struts2、Spring3,当我为我的应用程序进行 jUnit4 测试时,我收到了这个错误:

null java.lang.NullPointerException  
at org.apache.struts2.StrutsJUnit4TestCase.executeAction(StrutsJUnit4TestCase.java:134)     
at com.junitaction.HelloWorldTest.testGetString2(HelloWorldTest.java:76)    
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)   
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)     
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)   
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)   
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)    
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)     
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)   
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)

当我调用该executeAction方法时发生错误。如何解决这个问题。

我的 jar 文件:

antlr-2.7.2.jar、asm-3.3.jar、commons-fileupload-1.3.jar、commons-io-2.0.1.jar、commons-lang-2.4.jar、commons-lang3-3.1.jar、commons- logging-1.1.3.jar、freemarker-2.3.19.jar、javassist-3.11.0.GA.jar、ognl-3.0.6.jar、org.springframework.web.servlet-3.2.3.RELEASE.jar、 spring-aop-3.2.3.RELEASE.jar,spring-aspects-3.2.3.RELEASE.jar,spring-beans-3.2.3.RELEASE.jar,spring-context-3.2.3.RELEASE.jar,spring-上下文支持-3.2.3.RELEASE.jar、spring-core-3.2.3.RELEASE.jar、spring-expression-3.2.3.RELEASE.jar、spring-test-3.2.3.RELEASE.jar、spring- web-3.2.3.RELEASE.jar、struts2-core-2.3.15.1.jar、struts2-junit-plugin-2.3.15.1.jar、struts2-spring-plugin-2.3.15.1.jar、xwork-core-2.3。 15.1.jar。

struts.xml

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

 <struts>        
     <constant name="struts.objectFactory" 
     value="org.apache.struts2.spring.StrutsSpringObjectFactory"/>
         <!-- Configuration for the default package. -->
         <package name="default" extends="struts-default">
             <action name="helloworld" class="helloWorldClass">
                 <result>/success.jsp</result>
             </action>
         </package> 
 </struts>

web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="3.0" 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_3_0.xsd">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>            
            /WEB-INF/applicationContext.xml
        </param-value>
    </context-param>
    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list> 
</web-app>

appplicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <bean id="helloWorldClass" class="com.junitaction.HelloWorld" >
    </bean>
</beans>
4

1 回答 1

1

struts2 junit 插件添加到项目中并从StrutsSpringJUnit4TestCase.

struts.xml应该使用

<constant name="struts.objectFactory" value="spring" />

使弹簧能够管理您的操作。

于 2013-09-02T13:08:22.413 回答