Spring @ContextConfiguration how to put the right location for the xml
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 ....}
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:
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" })