Spring @ContextConfiguration how to set the correct location for xml

In our project, we write out a test to check whether the controller returned the correct representation of the model

@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 a null exception.

Now we configure @contextconfiguration, but we donโ€™t know how to load the correct xml, which is located in src \ main \ webapp \ root \ WEB-INF \ root-context.xml

 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration public class TestOverzichtSenario{ .... 

This documentation is not clear enough to understand.

Any suggestion on how to make sure contextannotation is loading the correct xml?

Change v2

I copied the .xml configuration files from the webINF folder to

 src\main\resources\be\..a bunch of folders..\configuration\*.xml 

and changed web.xml in webinf to

 <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) 
+43
java spring maven unit-testing annotations
Dec 07 '10 at 14:29
source share
9 answers

This is the reason why not to put the configuration in webapp .

As far as I know, there are no good ways to access files in the webapp folder from unit tests. You can put your configuration in src/main/resources instead, so you can access it from your unit tests (as described in the docs), as well as from webapp (using the classpath: prefix in contextConfigLocation ).

See also:

+37
Dec 07 2018-10-14
source share

Our tests look like this (using Maven and 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 .... } 
+66
Mar 17 2018-12-12T00:
source share

This is a definite maven problem, I think. Maven does not copy the /src/main/resources files to the target test folder. You will need to do this yourself by setting up the resource plugin if you absolutely want to go this route.

An easier way is to install a test definition of a specific context instead in the /src/test/resources directory and load via:

 @ContextConfiguration(locations = { "classpath:mycontext.xml" }) 
+19
Dec 07 2018-10-14T00:
source share

A simple solution is

 @ContextConfiguration(locations = { "file:src/main/webapp/WEB-INF/applicationContext.xml" }) 

from spring forum

+10
Jul 31 '13 at 7:19
source share

We use:

 @ContextConfiguration(locations="file:WebContent/WEB-INF/spitterMVC-servlet.xml") 

the project is a dynamic eclipse web project, then the way:

 {project name}/WebContent/WEB-INF/spitterMVC-servlet.xml 
+5
Jan 16 2018-12-12T00:
source share

Suppose you are going to create test-context.xml, which is independent of app-context.xml for testing, put test-context.xml in / src / test / resources. In the test class, add the @ContextConfiguration annotation on top of the class definition.

 @ContextConfiguration(locations = "/test-context.xml") public class MyTests { ... } 

Spring document Contextual Management

+4
Aug 03 '13 at 0:18
source share

Downloading file from: {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.. } } 
+2
Oct 17 '15 at 14:27
source share

@RunWith (SpringJUnit4ClassRunner.class) @ContextConfiguration (location = {"/Beans.xml"}) public class DemoTest {}

0
Apr 16 '15 at 7:32
source share

Sometimes it can be something rather simple, like skipping your resource file in the test classes folder due to some cleanup.

-one
Dec 14 '16 at 15:36
source share



All Articles