我在我的应用程序中使用 Struts2、Spring3 和 JPA。当我使用 junit 时,它按预期工作,但唯一的问题是用户定义的异常测试。
调用动作表单测试的代码是
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:ApplicationContext.xml"}) 公共类 HelloWorldTest 扩展 StrutsSpringJUnit4TestCase {
public HelloWorldTest() { }
> @Test
> public void testGetString2() throws Exception {
> HelloWorld helloWorld = new HelloWorld();
> String result = executeAction("/helloworld.action");
> String s = helloWorld.getString("Test");
> assertEquals("Test", this);
> }
我的WEB.XML文件。
> <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>
和我的 applicationContext 文件
> <beans>
> <bean id="helloWorldClass" class="com.junitaction.HelloWorld" >
> </bean>
> </beans>
得到错误之类的。
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)
Struts.xml文件
<struts>
<!-- Configuration for the default package. -->
<package name="default" extends="struts-default">
<action name="helloworld" class="helloWorldClass">
<result>/success.jsp</result>
</action>
</package>
</struts>
我如何从junit4调用actionClass ...请帮助我改进。我的 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。