Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 705
Tim Avatar asked Oct 11 '10 15:10

Tim


People also ask

What is Spring ApplicationContext XML?

Applicationcontext. xml - It is standard spring context file which contains all beans and the configuration that are common among all the servlets. It is optional file in case of web app. Spring uses ContextLoaderListener to load this file in case of web application. Spring-servlet.

How can I get ApplicationContext in Spring boot?

Spring Boot injects the application context into the parameter of the setApplicationContext() method, where we get the Id of the Spring application. (The Id here is the name of the application.) In the Application , we create a bean, call its method and set up the Spring Boot application.

Where should ApplicationContext XML be placed?

It needs to be in the classpath. You can put the original editable instance anywhere (e.g. a config directory off the root) but then you will need to have your build management tool (e.g. Ant or Maven ) copy it into the classpath for the runtime.

How does ApplicationContext work in Spring?

The ApplicationContext Interface The Spring IoC container is responsible for managing the objects of an application. It uses dependency injection to achieve inversion of control. The interfaces BeanFactory and ApplicationContext represent the Spring IoC container.


1 Answers

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

like image 90
biasedbit Avatar answered Sep 19 '22 17:09

biasedbit