• Register
0 votes
5.1k 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 5 3
6,930 points

Please log in or register to answer this question.

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:

9 7 4
38,600 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 {
...
}

 

10 6 4
31,120 points

Related questions

0 votes
1 answer 848 views
848 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 68 views
68 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 171 views
171 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 11 views
11 views
Problem: org.apache.catalina.lifecycleexception: failed to start component [connector[http/1.1-8080]]
asked Feb 18 Lucky Guy 1.4k points
0 votes
1 answer 4 views
4 views
Problem: failed to destroy end point associated with protocolhandler.
asked Feb 17 Ethan ross 1.8k points
0 votes
1 answer 914 views
914 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, 2020 mphil 2.3k points
0 votes
1 answer 252 views
252 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, 2020 Raphael Pacheco 4.9k points
0 votes
2 answers 322 views
322 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
0 votes
1 answer 18 views
18 views
Problem: java.lang.IllegalStateException: Not allowed to start service Intent.
asked Feb 12 Shovo210 2.7k points
0 votes
1 answer 10 views
10 views
Problem: public int getRecordsCount() { String countQuery = "SELECT * FROM " + TABLE_LOGIN; SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.rawQuery(countQuery, null); if(cursor != null && !cursor.isClosed()){ cursor.close(); } / ... with java.lang.IllegalStateException: attempt to re-open an already-closed object: android.database.sqlite.SQLiteQuery (mSql = SELECT * FROM login)
asked Feb 8 ahmedadel9090 2.6k points