Im using Struts2, Spring3 and JPA for my application. When i use junit it works as expected but the only problem is user defined exception test.
the code for calling action form Test is
@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);
> }
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 file
<struts>
<!-- Configuration for the default package. -->
<package name="default" extends="struts-default">
<action name="helloworld" class="helloWorldClass">
<result>/success.jsp</result>
</action>
</package>
</struts>
how can i call actionClass from junit4... please help me to improve.
my jar files:
The problem is you are mixing JUnit3 and JUnit4... The StrutsSpringTestCase is a JUnit3 based testcase, where the @Test annotation is from JUnit4. Mixing those thins in a single testcase isn't going to work.
Either switch your whole testcase to a JUnit4 based setup (by extending the StrutsSpringJUnit4TestCase class as mentioned in the comments). Or rewrite your test method to be a JUnit3 based test (i.e. without annotations and a try/catch).