43

I am creating a Maven Spring project, which includes MVC, Data and Security. My Spring applicationContext-*.xml files are located at \src\main\resources\spring\

My TestCase is placed at \src\test\java\my\package\controller\ and its code is:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
    "classpath:spring/applicationContext.xml",
    "classpath:spring/applicationContext-jpa.xml",
    "classpath:spring/applicationContext-security.xml" })
public class MyControllerTest extends TestCase {
    @Autowired
    private MyController myController;

    @Test
    public void myMethod_test() {
    }
}

When I right click on the test class and run as JUnit, I get

java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:99)
at org.springframework.test.context.DefaultTestContext.getApplicationContext(DefaultTestContext.java:101)
at 
[...]
 Caused by: 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 
[...]

If I try to remove applicationContext.xml from the locations, I still get exactly the same error. The complete stack trace is this:

java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:99)
at org.springframework.test.context.DefaultTestContext.getApplicationContext(DefaultTestContext.java:101)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:331)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:213)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:290)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:292)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:233)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:87)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:176)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: 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
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:343)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:303)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:216)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:187)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:251)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadBeanDefinitions(AbstractGenericContextLoader.java:253)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:122)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60)
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:100)
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:250)
at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContextInternal(CacheAwareContextLoaderDelegate.java:64)
at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:91)
... 25 more
Caused by: java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:329)
... 37 more
Manu
  • 4,019
  • 8
  • 50
  • 94
  • 2
    Try to use `"classpath:/spring/applicationContext.xml"` – Jens Jul 16 '14 at 09:12
  • still same error with that – Manu Jul 16 '14 at 09:14
  • If using Eclipse/STS, right click on your project -> Properties -> Java Build Path -> Source tab. Do you have in there [project_name]/src/main/resources as a Source folder? – Andrei Stefan Jul 16 '14 at 09:19
  • 1
    I think you can use it without classpath: @ContextConfiguration(locations = { "/spring/applicationContext.xml", "/spring/applicationContext-jpa.xml", "/spring/applicationContext-security.xml" }) – Jens Jul 16 '14 at 09:22
  • yes, along with src/main/java and src/test/java – Manu Jul 16 '14 at 09:23
  • As a test, remove `"classpath:spring/applicationContext.xml",` from `@ContextConfiguration`. Does it give the same error for the other file? – Andrei Stefan Jul 16 '14 at 09:42
  • No, it actually does give the same error, but still referring to applicationContext.xml... what is it looking for then? – Manu Jul 16 '14 at 09:45
  • So, "applicationContext.xml" is not referenced anymore, but you still get the exception for it? Update your questions and post the complete stack trace. – Andrei Stefan Jul 16 '14 at 09:50
  • Done. If I add the spring folder to the source folder, then I get a different error, which I also reported in the question. – Manu Jul 16 '14 at 09:56
  • I see it's the same error... – Andrei Stefan Jul 16 '14 at 09:57
  • What does `web.xml` have to do with your test? I think some kind of refreshing failure is going on with your IDE, related to tests, classpath resources or maybe compilation. Some of the behaviors described simply don't make sense in a normal Java program execution: you remove `applicationContext.xml` from definition and the error is the same. – Andrei Stefan Jul 16 '14 at 10:18
  • I referred to the web.xml because when I deploy the application on the server I don't get any error concerning global-method-security. Therefore, the problem must be into the test configuration. – Manu Jul 16 '14 at 10:20
  • ok, clean and build seemed to have fixed, so the solution was to add spring to the build path – Manu Jul 16 '14 at 10:26
  • if you are using maven, try run the test again in console – Jaiwo99 Jul 16 '14 at 11:00

11 Answers11

25

Try with the relative path using *

  @ContextConfiguration(locations = {
"classpath*:spring/applicationContext.xml",
"classpath*:spring/applicationContext-jpa.xml",
"classpath*:spring/applicationContext-security.xml" })

If not look if your xml are really on resources/spring/.

Finally try just on without location

 @ContextConfiguration({"classpath*:spring/applicationContext.xml"})

The other error that you´re showing is because you have this tag duplicated on applicationContext.xml and applicationContext-security.xml

 Duplicate <global-method-security>
paul
  • 12,873
  • 23
  • 91
  • 153
  • Your solutions work only after adding spring to the build path – Manu Jul 16 '14 at 10:27
  • @ContextConfiguration(locations = { "classpath*:resources/invoices-context.xml","classpath*:resources/invoices-int-schema.xml" }) This has worked for me ....Thanks – Ashutosh Shukla Mar 09 '16 at 16:18
13

I added the spring folder to the build path and, after clean&build, it worked.

Manu
  • 4,019
  • 8
  • 50
  • 94
  • How to add Spring folder to build path ? – abhijitcaps Sep 03 '15 at 11:51
  • 5
    Hi @abhijitcaps You can add spring folder to build path by right click on project and choose build path -> configure build path.. choose source folder and add path to the spring build path... – Thanh Duy Ngo Sep 07 '15 at 06:48
7

If you are using intellij, then try restarting intellij cache

  1. File-> Invalidate cache/restart
  2. clean and build project

See if it works, it worked for me.

Akanksha gore
  • 558
  • 1
  • 8
  • 7
6

try as below

@ContextConfiguration (locations = "classpath*:/spring/applicationContext*.xml")

this will load all 3 of your application context xml file.

Naveen Kumar
  • 893
  • 11
  • 19
5

For me, I was missing @ActiveProfile at my test class

@ActiveProfiles("sandbox")
class MyTestClass...
MCMatan
  • 8,623
  • 6
  • 46
  • 85
2

Give the below

@ContextConfiguration(locations =  {"classpath*:/spring/test-context.xml"})

And in pom.xml give the following plugin:

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-surefire-plugin</artifactId>
   <version>2.20.1</version>
<configuration>
    <additionalClasspathElements>
       <additionalClasspathElement>${basedir}/src/test/resources</additionalClasspathElement>
    </additionalClasspathElements>
</configuration>

Nicomedes E.
  • 1,326
  • 5
  • 18
  • 27
Seba anna
  • 21
  • 4
2

Add in in pom.xml give the following plugin:

<plugin>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.10</version>
  <configuration>
    <useFile>false</useFile>
  </configuration>
</plugin>
Tiago Viana
  • 91
  • 1
  • 1
1

I had the same problem and none of the previous answers have solved the issue for me.
I was getting the error due to the coexistence of spring-boot-starter-webflux and spring-boot-starter-tomcat in my pom.xml.
Excluding spring-boot-starter-tomcat from Test phase have solved the issue. I used maven-surefire-plugin to do so :

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <classpathDependencyExcludes>
                        <classpathDependencyExclude>org.springframework.boot:spring-boot-starter-tomcat</classpathDependencyExclude>
                    </classpathDependencyExcludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

spring-boot-starter-parent version : 2.6.3

Rayen Rejeb
  • 311
  • 2
  • 8
0

I faced the same error and realized that pom.xml had java 1.7 and STS compiler pointed to Java 1.8. Upon changing compiler to 1.7 and rebuild fixed the issue.

PS: This answer is not related to actual question posted but applies to similar error for app Context not loading

0

Check your run debug configuration. This value must be your main Spring Boot Aplication classenter image description here

if different class you can YourMainAplication.java --> Right Click --> Run

Thats it!

Ferhat KOÇER
  • 3,890
  • 1
  • 26
  • 26
-10

The problem is insufficient memory to load context.

Try to set VM options:

-da -Xmx2048m -Xms1024m -XX:MaxPermSize=2048m
ib11
  • 2,530
  • 3
  • 22
  • 55
codebee
  • 33
  • 5