我在我的应用程序中使用Struts2,Spring3和JPA。当我使用junit时,它可以按预期工作,但是唯一的问题是用户定义的异常测试。
调用操作表单Test的代码是
@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>
出现错误之类的。
org.apache.struts2.StrutsJUnit4TestCase.executeAction(StrutsJUnit4TestCase.java:134)处com.junitaction.HelloWorldTest.testGetString2(HelloWorldTest.java:76)处org.springframework.test.context.junit4.statements处的java.lang.NullPointerException org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)位于org.springframework.test.context.junit4.statements(SpringRepeat.evaluate)的RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)位于org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)的org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)的SpringRepeat.java:72) .springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks。在org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)处评估(RunBeforeTestClassCallbacks.java:61) 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- 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-core-2.3。 15.1.jar。
问题是您正在混合JUnit3和JUnit4 ...这StrutsSpringTestCase
是一个基于JUnit3的测试用例,其中的@Test
注释来自JUnit4。在单个测试用例中混合使用那些稀薄对象是行不通的。
要么将整个测试用例切换到基于JUnit4的设置(通过扩展StrutsSpringJUnit4TestCase
注释中提到的类)。或者将您的测试方法重写为基于JUnit3的测试(即,不带注释和try / catch)。
public void testSomeMethod() throws AccessDeniedException {
try {
throw new health.exception.AccessDeniedException("AccessDeniedException");
} catch (Exception e) {
Assert.assertTrue(e instanceof health.exception.AccessDeniedException);
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句