Meaningless Drivel is fun!*
The moose likes Spring and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark ""Failed to load ApplicationContext" exception when trying to run JUnit Spring test" Watch ""Failed to load ApplicationContext" exception when trying to run JUnit Spring test" New topic
Author

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

Dave Alvarado
Ranch Hand

Joined: Jul 02, 2008
Posts: 436
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

Joined: Feb 05, 2001
Posts: 17259
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Dave Alvarado
Ranch Hand

Joined: Jul 02, 2008
Posts: 436
The solution that ended up working for me was



Code on, - Dave
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

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
 
Don't get me started about those stupid light bulbs.
 
subject: "Failed to load ApplicationContext" exception when trying to run JUnit Spring test