• Register
0 votes
3.2k views

Problem :

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

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:

38.6k 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 {
...
}

 

31.1k points

Related questions

0 votes
1 answer 637 views
637 views
Problem : I am getting bellow error while executing JUNIT code java.lang.illegalstateexception no last call on a mock available
asked Nov 5, 2019 peterlaw 6.9k points
0 votes
1 answer 60 views
60 views
Problem : I am trying to run Java Applet and getting bellow error. java.lang.illegalstateexception: failed to create a child event loop
asked Oct 31, 2019 peterlaw 6.9k points
0 votes
1 answer 142 views
142 views
Problem : I'm trying to run Java Applet and every time I try to run program, I got this this error. error: java.lang.illegalstateexception: failed to create a child event loop
asked Oct 23, 2019 peterlaw 6.9k points
0 votes
1 answer 574 views
574 views
Problem : Currently I am trying to develop the REST API using the Spring Framework I am unable to run my application because of below error. Description: The Tomcat connector configured to listen on port 8080 failed to start. The port may already be in use or the connector may ... this application to listen on another port. My 8080 port on my PC is seems to be busy. How can I fix this issue?
asked Feb 28 mphil 2.3k points
0 votes
1 answer 68 views
68 views
Problem: I have recently started working on spring jdbc application. I am very new to spring and also developing spring jdbc application. Because of some reason my driver class is not getting loaded. I am facing below error: "org.springframework.beans. ... jdbcTemplate" ref="jdbcTemplate"></property> </bean> </beans> Can someone help me in fixing above spring related error?
asked Jul 14 Raphael Pacheco 4.9k points