简体   繁体   English

junit找不到Web应用程序上下文:没有注册ContextLoad侦听器?

[英]junit no webapplication context found: no ContextLoad Listener registered?

I am trying to write test cases for my structs application in which some of configurations in springs based xml files. 我正在尝试为我的structs应用程序编写测试用例,其中基于springs的xml文件中的一些配置。 i moved all my applicationContext files into src/test/resources class path but when i run my test class(RunAs-> Junit Test), it throws below error 我将所有applicationContext文件都移到src / test / resources类路径中,但是当我运行测试类(RunAs-> Junit Test)时,它抛出以下错误

java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?
    at org.springframework.web.context.support.WebApplicationContextUtils.getRequiredWebApplicationContext(WebApplicationContextUtils.java:90)
    at com.cisco.attrui.test.BundleSearchActionTest.getPidBTSplitPctSearchTest(BundleSearchActionTest.java:69)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
    at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

Seems like similar problem as in other post I recently answered here: JUnit 4.12: testing util classes 好像和其他我最近在这里回答的帖子类似的问题: JUnit 4.12:测试util类

Moving xml configs to classpath is not enough. 仅将xml配置移动到classpath是不够的。 You need to tell Spring which ones to use and you need to use Spring test runner in the first place. 您需要告诉Spring要使用哪些工具,并且首先需要使用Spring测试运行程序。

Like this: 像这样:

@ContextConfiguration(locations = {"/integrationContextTest.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class TdkSMSSenderTest  {
}

If the files are in src/test/resources, you can add the @ContextConfiguration annotation to your test class to fetch as many files as required by providing them in an array follows: 如果文件位于src / test / resources中,则可以将@ContextConfiguration批注添加到测试类中,以通过在数组中提供以下文件来获取所需数量的文件:

@ContextConfiguration(locations = {"classpath:one-file.xml","classpath:another-file.xml", "classpath:and-another-file.xml"})
@RunWith(SpringJUnit4ClassRunner.class)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM