문제

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.

도움이 되었습니까?

해결책

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);
  }
}  
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top