Programmers learn & share
0 votes
267 views

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:

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

Further Readings:

by (36.1k points)  
0 votes

Solution:

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

@RunWith(SpringJUnit4ClassRunner.class)
@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

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

 

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