• Register
0 votes
7.3k 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 65 views
65 views
Problem : When I try to run my Maven Spring project as JUnit i get bellow error java.lang.illegalstateexception: failed to load applicationcontext
asked Mar 6 Wafa Abu Yousef 6.1k points
0 votes
1 answer 988 views
988 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
1 vote
1 answer 2 views
2 views
Problem: Any clue to this? Java.lang.illegalstateexception: response has already been committed.
asked 5 days ago PkGuy 23.5k points
0 votes
1 answer 10 views
10 views
Problem: Is anyone available to assist me and resolve this issue? java.lang.illegalstateexception: cannot call senderror() after the response has been committed
asked Apr 7 Ifra 36.4k points
0 votes
1 answer 5 views
5 views
Problem: I am trying to write a JUnit test case for the same purpose. But on line Session session = Session.getDefaultInstance(properties); I am getting the following exception: java.lang.NoClassDefFoundError: com/sun/mail/util/MailLogger
asked Apr 24 ummeshani 9.5k points
0 votes
1 answer 8 views
8 views
Problem: How to fix request processing failed; nested exception is java.lang.NullPointerException?
asked Apr 15 sumaiya simi 43.9k points
1 vote
1 answer 4 views
4 views
Problem: Any help will be appreciated to solve this error: Exception in thread "main" java.util.inputmismatchexception.
asked Apr 26 PkGuy 23.5k points
1 vote
1 answer 4 views
4 views
Problem: How to get rid of this error? Exception in thread "main" java.util.nosuchelementexception.
asked Apr 25 PkGuy 23.5k points
0 votes
1 answer 9 views
9 views
Problem: I'm trying to call a controller method that will save my object, but when i try to url to it, it returns http error. I've browsed through some similar problems on SO but no luck.
asked 5 days ago Humaira ahmed 50.7k points
0 votes
1 answer 8 views
8 views
Problem: I'm trying to call a controller method that will save my object, but when i try to url to it, it returns http error. I've browsed through some similar problems on SO but no luck. So i wanna ask myself... Here is my Ajax (for simplicity purposes i renamed the ... function (data) { console.log(json); alert(data.message); resetForm(); }, error: function () { alert("Error!"); } }); }
asked Apr 26 sumaiya simi 43.9k points