• Register
0 votes
1.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:

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

 

17.7k points

Related questions

0 votes
1 answer 409 views
409 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 45 views
45 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 101 views
101 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 171 views
171 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
2 answers 107 views
107 views
Problem : I am facing bellow error java.sql.sqlrecoverableexception no more data to read from socket
asked Nov 5, 2019 peterlaw 6.9k points