jpa - Junit4 throws an Exception when test run -
this question has answer here:
im using struts2, spring3 , jpa application. when use junit works expected problem user defined exception test.
the code calling action form test
@runwith(springjunit4classrunner.class) @contextconfiguration(locations = {"classpath:applicationcontext.xml"}) public class helloworldtest extends 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); > }
my web.xml file.
> <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>
and applicationcontext file
> <beans> > <bean id="helloworldclass" class="com.junitaction.helloworld" > > </bean> > </beans>
getting error like.
java.lang.nullpointerexception @ org.apache.struts2.strutsjunit4testcase.executeaction(strutsjunit4testcase.java:134) @ com.junitaction.helloworldtest.testgetstring2(helloworldtest.java:76) @ org.springframework.test.context.junit4.statements.runbeforetestmethodcallbacks.evaluate(runbeforetestmethodcallbacks.java:74) @ org.springframework.test.context.junit4.statements.runaftertestmethodcallbacks.evaluate(runaftertestmethodcallbacks.java:83) @ org.springframework.test.context.junit4.statements.springrepeat.evaluate(springrepeat.java:72) @ org.springframework.test.context.junit4.springjunit4classrunner.runchild(springjunit4classrunner.java:231) @ org.springframework.test.context.junit4.springjunit4classrunner.runchild(springjunit4classrunner.java:88) @ org.springframework.test.context.junit4.statements.runbeforetestclasscallbacks.evaluate(runbeforetestclasscallbacks.java:61) @ org.springframework.test.context.junit4.statements.runaftertestclasscallbacks.evaluate(runaftertestclasscallbacks.java:71) @ org.springframework.test.context.junit4.springjunit4classrunner.run(springjunit4classrunner.java:174)
struts.xml file
<struts> <!-- configuration default package. --> <package name="default" extends="struts-default"> <action name="helloworld" class="helloworldclass"> <result>/success.jsp</result> </action> </package> </struts>
how can call actionclass junit4... please me improve. jar files:
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.
the problem mixing junit3 , junit4... strutsspringtestcase
junit3 based testcase, @test
annotation junit4. mixing thins in single testcase isn't going work.
either switch whole testcase junit4 based setup (by extending strutsspringjunit4testcase
class mentioned in comments). or rewrite test method junit3 based test (i.e. without annotations , try/catch).
public void testsomemethod() throws accessdeniedexception { try { throw new health.exception.accessdeniedexception("accessdeniedexception"); } catch (exception e) { assert.asserttrue(e instanceof health.exception.accessdeniedexception); } }
Comments
Post a Comment