0

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>
daydreamer
  • 87,243
  • 191
  • 450
  • 722
  • applicationContext.xml has to be in the WEB-INF folder. Are you sure is not there? – DarkCoffee Jul 17 '14 at 15:55
  • Its at `WEB-INF/classes/applicationContext.xml` as mentioned in my question – daydreamer Jul 17 '14 at 15:56
  • Try to put the file in the WEB-INF folder in order to have WEB-INF/applicationContext.xml – DarkCoffee Jul 17 '14 at 15:58
  • Ok, how do you get your applicationContext.xml into that folder? When your test runs, it's not running against the packaged webapp, it's running against a classpath, and copied to WEB-INF/classes during the build. However, if the applicationContext.xml is in the root of the resources folder, it will be found on the classpath. If, however, you've manually placed it in WEB-INF/classes in your source, then you're SOL -- it will not be on the classpath for your tests. – Software Engineer Jul 17 '14 at 15:58
  • @DarkCoffee -- that won't work. It needs to be on the classpath, not in WEB-INF... – Software Engineer Jul 17 '14 at 15:59
  • @EngineerDollery, I got it there by packaging it as `war` – daydreamer Jul 17 '14 at 16:02
  • @daydreamer -- cool, but where does the applicationContext.xml file live in your source code? You see, running your tests has absolutely nothing to do with the packaged war. – Software Engineer Jul 17 '14 at 16:03
  • `services/src/main/resources/applicationContext.xml` – daydreamer Jul 17 '14 at 16:04
  • also added what my `web.xml` has about `contextConfiguration` – daydreamer Jul 17 '14 at 16:08
  • @daydreamer Put the xml file in src/main/resources and try with ContextConfiguration (locations = "/applicationContext.xml") – Prasad Jul 17 '14 at 18:16

1 Answers1

0

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.

oussama.elhadri
  • 738
  • 3
  • 11
  • 27