简体   繁体   中英

Spring: Can not find applicationContext even though it's present in war file

My test looks like

public class ITCache extends AbstractIntegrationTest {
    @Test
    public void readCache() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        assertNotNull(applicationContext);
        final SimpleCacheManager simpleCacheManager = (SimpleCacheManager) applicationContext.getBean("cacheManager");
        System.out.println("cacheName:" + simpleCacheManager.getCacheNames().toString());
        System.out.println(simpleCacheManager.getCache("genders").toString());
    }
}

I deploy my project using maven-cargo plugin and can see the contents and find applicationContext.xml as well

➜  comma git:(S120297) ✗ jar -tvf integration/target/cargo/configurations/tomcat7x/webapps/common.war | grep app
  2342 Wed Jul 16 20:28:32 PDT 2014 WEB-INF/classes/applicationContext.xml
➜  comma git:(S120297) ✗  

But when I run test, I see failures as

Tests in error: 
  readCache(com.org.comma.integration.ITCache): IOException parsing XML document from class path resource [applicationContext.xml]; nested exception is java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist  

I also tried annotating my test as

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration (locations = "classpath*:/applicationContext*.xml")
public class ITCache extends AbstractIntegrationTest {  

but still same issue

What is going wrong here?

UPDATE
My web.xml has entry as following

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

as the error said ,the file is not found in your classpath , make sure that your file is in the root of your classpath , you can add your applicationContext file to the resource folder and launch install. i have the same code and it's work , the web.xml have nothing to do with launching the test , other thing how you launch tests is important you can do that threw maven or directly with eclipse. we you launch test with maven it's generate resources unlike the other way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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