org.apache.struts2.StrutsJUnit4TestCase.executeAction(StrutsJUnit4TestCase.java:134)中的java.lang.NullPointerException

时间:2013-09-02 13:00:55

标签: java struts2 nullpointerexception junit4 spring-3

我正在为我的应用程序使用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-context-support-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的核 - 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>

1 个答案:

答案 0 :(得分:1)

将struts2 junit插件添加到项目中,并从StrutsSpringJUnit4TestCase扩展测试。

struts.xml中你应该使用

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

让春天能够管理你的行动。