Java Spring @ContextConfiguration 如何为 xml 放置正确的位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4377699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring @ContextConfiguration how to put the right location for the xml
提问by David
In our project we are writting a test to check if the controller returns the right modelview
在我们的项目中,我们正在编写一个测试来检查控制器是否返回正确的模型视图
@Test
public void controllerReturnsModelToOverzichtpage()
{
ModelAndView modelView = new ModelAndView();
KlasoverzichtController controller = new KlasoverzichtController();
modelView = controller.showOverzicht();
assertEquals("Klasoverzichtcontroller returns the wrong view ", modelView.getViewName(), "overzicht");
}
This returns the exception null.
这将返回异常 null。
We are now configuring the @contextconfiguration but we don't know how to load the right xml who is located at src\main\webapp\root\WEB-INF\root-context.xml
我们现在正在配置@contextconfiguration,但我们不知道如何加载位于src\main\webapp\root\WEB-INF\root-context.xml 的正确xml
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class TestOverzichtSenario{
....
This documentation isn't clear enough to understand
Any suggestion on how to make sure the contextannotation loads the right xml?
关于如何确保上下文注释加载正确的 xml 的任何建议?
Edit v2
编辑 v2
I copied the configuration .xml files from the webINF folder to
我将配置 .xml 文件从 webINF 文件夹复制到
src\main\resources\be\..a bunch of folders..\configuration\*.xml
and changed the web.xml in webinf to
并将 webinf 中的 web.xml 更改为
<param-name>contextConfigLocation</param-name>
<param-value>
classpath*:configuration/root-context.xml
classpath*:configuration/applicationContext-security.xml
</param-value>
and now get the error
现在得到错误
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/mvc-dispatcher-servlet.xml]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/mvc-dispatcher-servlet.xml]
org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:341)
org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143)
org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178)
org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149)
org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:124)
org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:93)
org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:130)
org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:467)
org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:397)
org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:442)
org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:458)
org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:339)
org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:306)
org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:127)
javax.servlet.GenericServlet.init(GenericServlet.java:212)
com.springsource.insight.collection.tcserver.request.HttpRequestOperationCollectionValve.invoke(HttpRequestOperationCollectionValve.java:80)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:379)
java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
java.lang.Thread.run(Unknown Source)
采纳答案by axtavt
That's the reason not to put configuration into webapp
.
这就是不将配置放入webapp
.
As far as I know, there are no good ways to access files in webapp
folder from the unit tests. You can put your configuration into src/main/resources
instead, so that you can access it from your unit tests (as described in the docs), as well as from the webapp (using classpath:
prefix in contextConfigLocation
).
据我所知,没有好的方法可以webapp
从单元测试中访问文件夹中的文件。您可以src/main/resources
改为将您的配置放入其中,以便您可以从单元测试(如文档中所述)以及从 webapp(使用classpath:
前缀 in contextConfigLocation
)访问它。
See also:
也可以看看:
回答by fasseg
This is a maven specific problem I think. Maven does not copy the files form /src/main/resources
to the target-test folder. You will have to do this yourself by configuring the resources plugin, if you absolutely want to go this way.
我认为这是一个 maven 特定问题。Maven 不会将文件形式复制/src/main/resources
到目标测试文件夹。如果您绝对想这样做,则必须通过配置资源插件自己来完成此操作。
An easier way is to instead put a test specific context definition in the /src/test/resources
directory and load via:
一种更简单的方法是将测试特定的上下文定义放在/src/test/resources
目录中并通过以下方式加载:
@ContextConfiguration(locations = { "classpath:mycontext.xml" })
回答by Grubhart
We Use:
我们用:
@ContextConfiguration(locations="file:WebContent/WEB-INF/spitterMVC-servlet.xml")
the project is a eclipse dynamic web project, then the path is:
项目是eclipse动态web项目,那么路径是:
{project name}/WebContent/WEB-INF/spitterMVC-servlet.xml
回答by bernd
Our Tests look like this (using Maven and Spring 3.1):
我们的测试看起来像这样(使用 Maven 和 Spring 3.1):
@ContextConfiguration
(
{
"classpath:beans.xml",
"file:src/main/webapp/WEB-INF/spring/applicationContext.xml",
"file:src/main/webapp/WEB-INF/spring/dispatcher-data-servlet.xml",
"file:src/main/webapp/WEB-INF/spring/dispatcher-servlet.xml"
}
)
@RunWith(SpringJUnit4ClassRunner.class)
public class CCustomerCtrlTest
{
@Resource private ApplicationContext m_oApplicationContext;
@Autowired private RequestMappingHandlerAdapter m_oHandlerAdapter;
@Autowired private RequestMappingHandlerMapping m_oHandlerMapping;
private MockHttpServletRequest m_oRequest;
private MockHttpServletResponse m_oResp;
private CCustomerCtrl m_oCtrl;
// more code ....
}
回答by Bala
Simple solution is
简单的解决办法是
@ContextConfiguration(locations = { "file:src/main/webapp/WEB-INF/applicationContext.xml" })
from spring forum
来自春季论坛
回答by Dino Tw
Suppose you are going to create a test-context.xml which is independent from app-context.xml for testing, put test-context.xml under /src/test/resources. In the test class, have the @ContextConfiguration annotation on top of the class definition.
假设您要创建一个独立于 app-context.xml 的 test-context.xml 进行测试,将 test-context.xml 放在 /src/test/resources 下。在测试类中,在类定义的顶部有 @ContextConfiguration 注释。
@ContextConfiguration(locations = "/test-context.xml")
public class MyTests {
...
}
Spring document Context management
Spring文档上下文管理
回答by Deepu Surendran
@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = {"/Beans.xml"}) public class DemoTest{}
@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = {"/Beans.xml"}) 公共类 DemoTest{}
回答by Arpit Aggarwal
Loading the file from: {project}/src/main/webapp/WEB-INF/spring-dispatcher-servlet.xml
从以下位置加载文件: {project}/src/main/webapp/WEB-INF/spring-dispatcher-servlet.xml
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "file:src/main/webapp/WEB-INF/spring-dispatcher-servlet.xml" })
@WebAppConfiguration
public class TestClass {
@Test
public void test() {
// test definition here..
}
}
回答by gpushkas
Sometimes it might be something pretty simple like missing your resource file in test-classses folder due to some cleanups.
有时它可能很简单,比如由于一些清理而丢失了 test-classses 文件夹中的资源文件。