Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Project: Failed to load ApplicationContext

I have a Java Project in which I am writing a simple JUNIT test case. I have copied the applicatinoContext.xml file into the root java source directory. I've tried it with some of the recommended settings I have read of here on StackOverflow but still get the same error. Is this error happening due to my project being a java project and NOT a web project, or does that even matter? I'm not sure where im going wrong.

import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.transaction.annotation.Transactional;  @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"C:/projs/sortation/src/main/java/applicationContext.xml"}) // Also tried these settings but they also didnt work, //@ContextConfiguration(locations={"classpath:applicationContext.xml"}) //@ContextConfiguration("classpath:applicationContext.xml") @Transactional public class TestSS {      @Autowired     private EmsDao dao;      @Test     public void getSites() {          List<String> batchid = dao.getList();          for (String s : batchid) {             System.out.println(s);         }     } } 
like image 842
Byron Avatar asked Feb 20 '11 16:02

Byron


People also ask

What is ApplicationContext failed to load?

However, sometimes in this situation, we may encounter the application context loading error “Failed to load ApplicationContext.” This error appears in the test classes because the application context isn't loaded in the test context.

What is an ApplicationContext?

ApplicationContext is an interface for providing configuration information to an application. There are multiple classes provided by springframework that implements this interface and helps us use configuration information in applications. ApplicationContext provides standard bean factory lifecycle capabilities.

How can you access the application context in a Spring integration test?

By default the ApplicationContext is loaded using the GenericXmlContextLoader which loads a context from XML Spring configuration files. You can then access beans from the ApplicationContext by annotating fields in your test class with @Autowired , @Resource , or @Inject .


2 Answers

Looks like you are using maven (src/main/java). In this case put the applicationContext.xml file in the src/main/resources directory. It will be copied in the classpath directory and you should be able to access it with

@ContextConfiguration("/applicationContext.xml") 

From the Spring-Documentation: A plain path, for example "context.xml", will be treated as a classpath resource from the same package in which the test class is defined. A path starting with a slash is treated as a fully qualified classpath location, for example "/org/example/config.xml".

So it's important that you add the slash when referencing the file in the root directory of the classpath.

If you work with the absolute file path you have to use 'file:C:...' (if I understand the documentation correctly).

like image 151
FrVaBe Avatar answered Sep 19 '22 17:09

FrVaBe


I had the same problem, and I was using the following plugin for tests:

<plugin>     <groupId>org.apache.maven.plugins</groupId>     <artifactId>maven-surefire-plugin</artifactId>     <version>2.9</version>     <configuration>         <useFile>true</useFile>         <includes>             <include>**/*Tests.java</include>             <include>**/*Test.java</include>         </includes>         <excludes>             <exclude>**/Abstract*.java</exclude>         </excludes>         <junitArtifactName>junit:junit</junitArtifactName>         <parallel>methods</parallel>         <threadCount>10</threadCount>     </configuration> </plugin> 

The test were running fine in the IDE (eclipse sts), but failed when using command mvn test.

After a lot of trial and error, I figured the solution was to remove parallel testing, the following two lines from the plugin configuration above:

    <parallel>methods</parallel>     <threadCount>10</threadCount> 

Hope that this helps someone out!

like image 40
Matyas Avatar answered Sep 22 '22 17:09

Matyas