Junit4在测试运行时引发异常

Hariprasath

我在我的应用程序中使用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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何通过Mocha测试运行时参数?

来自分类Dev

通过Jest获取测试运行时间

来自分类Dev

如何通过Mocha测试运行时参数?

来自分类Dev

具有预期异常的测试将产生以下错误:“测试运行时,代理进程已停止。”

来自分类Dev

当我取消单元测试运行时,当前运行的测试是否完成?

来自分类Dev

使用JUNIT测试运行AsyncHttpClient

来自分类Dev

当整个Maven项目在junit测试下运行时,logger.info引发空指针异常

来自分类Dev

当整个Maven项目在junit测试下运行时,logger.info引发空指针异常

来自分类Dev

java-仅在测试运行时如何忽略类或方法

来自分类Dev

在测试运行时类型检查期间如何模拟Python类?

来自分类Dev

_XCTFailInCurrentTest应该仅在测试运行时调用

来自分类Dev

KIF测试运行时应用崩溃,并且未报告任何故障

来自分类Dev

将Maven依赖声明为仅测试运行时

来自分类Dev

dotnet test命令显示单个测试运行时间?

来自分类Dev

cypress 测试运行时无法加载地图和 css 的图块

来自分类Dev

如何使用在测试运行时确定的类来创建特定GUI类的测试对象?

来自分类Dev

如何使Tycho将平台特定的片段加载到任何OS的测试运行时中?

来自分类Dev

作为插件测试运行时,不同项目中重复的软件包名称会导致InvalidAccessException

来自分类Dev

通过 spring boot 测试运行时,控制器中未发生 Bean 注入

来自分类Dev

从cmd运行junit4测试

来自分类Dev

如何在Eclipse中使用运行方式> SWTBot测试运行JUnit 5测试

来自分类Dev

带有Spring Boot JUnit 5测试的Wiremock:测试运行后正在使用的地址

来自分类Dev

着色golang测试运行输出

来自分类Dev

作为Maven测试运行

来自分类Dev

再现ScalaCheck测试运行

来自分类Dev

Couchbase测试运行失败

来自分类Dev

Intellij测试运行历史

来自分类Dev

测试运行null和字母?

来自分类Dev

Serverspec测试运行问题

Related 相关文章

  1. 1

    如何通过Mocha测试运行时参数?

  2. 2

    通过Jest获取测试运行时间

  3. 3

    如何通过Mocha测试运行时参数?

  4. 4

    具有预期异常的测试将产生以下错误:“测试运行时,代理进程已停止。”

  5. 5

    当我取消单元测试运行时,当前运行的测试是否完成?

  6. 6

    使用JUNIT测试运行AsyncHttpClient

  7. 7

    当整个Maven项目在junit测试下运行时,logger.info引发空指针异常

  8. 8

    当整个Maven项目在junit测试下运行时,logger.info引发空指针异常

  9. 9

    java-仅在测试运行时如何忽略类或方法

  10. 10

    在测试运行时类型检查期间如何模拟Python类?

  11. 11

    _XCTFailInCurrentTest应该仅在测试运行时调用

  12. 12

    KIF测试运行时应用崩溃,并且未报告任何故障

  13. 13

    将Maven依赖声明为仅测试运行时

  14. 14

    dotnet test命令显示单个测试运行时间?

  15. 15

    cypress 测试运行时无法加载地图和 css 的图块

  16. 16

    如何使用在测试运行时确定的类来创建特定GUI类的测试对象?

  17. 17

    如何使Tycho将平台特定的片段加载到任何OS的测试运行时中?

  18. 18

    作为插件测试运行时,不同项目中重复的软件包名称会导致InvalidAccessException

  19. 19

    通过 spring boot 测试运行时,控制器中未发生 Bean 注入

  20. 20

    从cmd运行junit4测试

  21. 21

    如何在Eclipse中使用运行方式> SWTBot测试运行JUnit 5测试

  22. 22

    带有Spring Boot JUnit 5测试的Wiremock:测试运行后正在使用的地址

  23. 23

    着色golang测试运行输出

  24. 24

    作为Maven测试运行

  25. 25

    再现ScalaCheck测试运行

  26. 26

    Couchbase测试运行失败

  27. 27

    Intellij测试运行历史

  28. 28

    测试运行null和字母?

  29. 29

    Serverspec测试运行问题

热门标签

归档