Programmers learn & share
0 votes

Problem :

When I try to run my Maven Spring project as JUnit i get bellow error
java.lang.illegalstateexception: failed to load applicationcontext
by (6.9k points)   | 267 views

2 Answers

0 votes

Solution :

I made bellow changes and my problem got resolved.

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

And in pom.xml give the bellow plugin:


Further Readings:

by (36.1k points)  
0 votes


Try with the relative path using *
  @ContextConfiguration(locations = {
"classpath*:spring/applicationContext-security.xml" })

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

Finally try just on without location


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

Duplicate <global-method-security>

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

As mentioned in duscusion: WEB-INF is not really a part of class path. If you use a common template such as maven, use src/main/resources or src/test/resources to place the app-context.xml into. Then you can use 'classpath:'.

Place your config file into src/main/resources/app-context.xml and use code

@ContextConfiguration(locations = "classpath:app-context.xml")
public class PersonControllerTest {

or you can make yout test context with different configuration of beans.

Place your config file into src/test/resources/test-app-context.xml and use code

@ContextConfiguration(locations = "classpath:test-app-context.xml")
public class PersonControllerTest {


ago by (4.7k points)  
2,183 questions
2,484 answers
241 users