Spring @ContextConfiguration how to put the right location for the xml

198,926

Solution 1

That's the reason not to put configuration into 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).

See also:

Solution 2

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 ....
}

Solution 3

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.

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

@ContextConfiguration(locations = { "classpath:mycontext.xml" })

Solution 4

Simple solution is

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

from spring forum

Solution 5

We Use:

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

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

{project name}/WebContent/WEB-INF/spitterMVC-servlet.xml
Share:
198,926
David
Author by

David

Updated on July 09, 2022

Comments

  • David
    David almost 2 years

    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.

    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

    @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?

    Edit v2

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

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

    and changed the 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)
    
  • David
    David over 13 years
    Do you have any documentation references what "classpath: " does?
  • axtavt
    axtavt over 13 years
    @Dvd: I guess you need classpath:be/..a bunch of folders../configuration/root-context.xml and classpath:be/..a bunch of folders../configuration/applicationContext-security.xml
  • Andry
    Andry almost 11 years
    Thank you very much, it solves my problem! But do you think the approach: "file:path" is correct?
  • dan carter
    dan carter almost 11 years
    /src/main/resources should not be copied to the target/test-classes. Test classes
  • dan carter
    dan carter almost 11 years
    /src/main/resources should not be copied to the target/test-classes folder. test-classes is for test resources, i.e. src/test/java and src/test/resources. src/main/resources gets copied to target/classes and target/classes is placed on the classpath when running tests, and thus any spring context files in src/main/resources can be loaded in unit tests as a classpath resource e.g. src/main/resources/spring/my-context.xml can be loaded as @ContextConfiguration({"/spring/my-context.xml"})
  • fasseg
    fasseg almost 11 years
    @Dan: I'd say, that if you can e.g. reuse (parts of) the config from main in the test context, it makes sense to let maven copy the existing files for the tests using the maven-resources-plugin. So saying "should not" be copied is a bit too harsh. Of course Maven does not do this by default, but if you have good reasons to do so yourself...why not?
  • ahmehri
    ahmehri over 9 years
    @fasseg: What @Dan meant is that you don't need to copy the config from main to the test context, because this config is already provided to the unit tests (because target/classes is placed on the classpath when running them).