0

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);
>     }

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 my applicationContext file

> <beans>
>         <bean id="helloWorldClass" class="com.junitaction.HelloWorld" > 
>         </bean>
>     </beans>

Getting Error like.

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:

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.

Hariprasath
  • 828
  • 4
  • 15
  • 41
  • http://stackoverflow.com/questions/5550175/junit-test-expected-annotation-not-working – Roman C Aug 26 '13 at 09:49
  • if i use that code means i can get the test passed. but for my app i need to configure both struts and spring. if i used above extended class i cant get the Exception test passed, but all other action flow is get passed. – Hariprasath Aug 26 '13 at 10:00
  • Which version of Struts2? See if you can use `StrutsSpringJUnit4TestCase` class. – Aleksandr M Aug 26 '13 at 11:31
  • im using Struts2.3.14.3 i cant to change StrutsSpringTestCase abstract because all other modules are working in that class only. – Hariprasath Aug 26 '13 at 11:41

1 Answers1

2

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).

public void testSomeMethod() throws AccessDeniedException {
  try {
    throw new health.exception.AccessDeniedException("AccessDeniedException");
  } catch (Exception e) {
    Assert.assertTrue(e instanceof health.exception.AccessDeniedException);
  }
}  
M. Deinum
  • 115,695
  • 22
  • 220
  • 224
  • i think try catch is not a standard way to do the User Defined exception. the correct way to do the User defined Exception in Junit4 is shown in my code. – Hariprasath Aug 26 '13 at 13:09
  • For JUnit4 that is correct however as I mentioned you are mixing JUnit3 and JUnit4 and that simply isn't going to work. SO either switch to JUnit4 fully or use the JUnit3 approach (as I mentioned in my answer). – M. Deinum Aug 26 '13 at 13:53
  • +1 Note: Also in real test you probably need to use `Assert.fail()` after the line that might throw exception. – Aleksandr M Aug 26 '13 at 15:37
  • That would probably lead to a compilation error, it would be unreachable code. – M. Deinum Aug 26 '13 at 16:58
  • @M.Deinum: I meant in real life test where you call some method that might throw that exception. And not directly throwing it like in this example. – Aleksandr M Aug 26 '13 at 17:56
  • This was the code you posted and as such I modified the testcode. In JUnit3 you have to catch the exception and fail if there is no exception. In JUnit4 there are better ways but not in JUnit3. – M. Deinum Aug 27 '13 at 05:47
  • when i use StrutsSpringJUnit4TestCase, nullpointer exception exception is occuring. How can i resolve this exception. Im using executeAction method for action call. – Hariprasath Sep 02 '13 at 10:46
  • Post your new configuration (update your question) and setup. Without seeing it is hard to tell. I guess you are not using a spring managed instance of a bean where you should (creating a new one yourself for instance). ANd post the full stacktrace. – M. Deinum Sep 02 '13 at 10:50
  • i've updated my code, im getting nullpointer when calling action class. – Hariprasath Sep 02 '13 at 11:53
  • Either your action is doing something wrong (or nothing), assuming I looked at the correct source code, the `response.getContentAsString` method returns `null` (from `MockHttpServletResponse`) which leads me to believe your action isn't correct or wrongly configured. Make sure that your struts configuration is read and that it references the spring configured bean! Take a look at [this tutorial](http://www.mkyong.com/struts2/struts-2-spring-integration-example/). – M. Deinum Sep 02 '13 at 12:09
  • but i can run in browser, there is no errors in it – Hariprasath Sep 02 '13 at 12:13
  • is there any other way to call an action. – Hariprasath Sep 02 '13 at 12:15
  • Instead of snippets post your full testcase and stacktrace, also make sure that your struts configuration is correctly loaded by the testcase (by default it only loads the `struts-default.xml` file). – M. Deinum Sep 02 '13 at 12:23