Spring – applicationContext.xml cannot be opened because it does not exist

junitspringspring-mvc

I have a Spring MVC application and a problem with JUnit tests combined with the file applicationContext.xml.

In my JUnit test class I write:

final ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
service = (TestServiceImpl) context.getBean("testServiceImpl");

The error I get is that aplicationContect.xml can not be found:

org.springframework.beans.factory.BeanDefinitionStoreException: 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

But it exists in the WEB-INF folder.

So, what's wrong here? Why does the file not exist for the JUnit test?

Best Answer

You should keep your Spring files in another folder, marked as "source" (just like "src" or "resources").

WEB-INF is not a source folder, therefore it will not be included in the classpath (i.e. JUnit will not look for anything there).

Related Topic