• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

"Failed to load ApplicationContext" exception when trying to run JUnit Spring test

 
Dave Alvarado
Ranch Hand
Posts: 436
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a Maven (v 3.0.3) project using Spring 3.0.5.RELEASE. I want to write a mock Spring JUnit test to verify that one of my controllers is redirecting to the proper view. However, I can't get my application context to load properly. I have these files ...



Then in my Spring JUnit test, I have



However, upon running my JUnit test from Maven, I get the error



What is the proper way to reference my web.xml file? Thanks, - Dave
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't reference the web.xml. The web.xml file is not a Spring configuration file.

If you want to Unit Test your controller, you don't need to have Spring started up in your test. But you might have to stub or mock out any Web specific classes with something like HttpMock.

Your Controller should be a pojo, so you don't need a true web environment in your tests.

Also, those two files that are in WEB-INF. Well that isn't in your classpath. And you are trying to load them with classpath. However, you can't use http: either, since your tests will not be running in a Web Environment. So you will need to have a different config file in your test directory that is in your classpath.

Hope that helps.

Mark

 
Dave Alvarado
Ranch Hand
Posts: 436
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The solution that ended up working for me was



Code on, - Dave
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep that works.

Make sure you don't move the file to a different directory or that will then start failing again. ;)

You could also create another config file that you keep in the same package as your test class that is loaded only by your test class.

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic