This week's giveaway is in the Artificial Intelligence and Machine Learning forum.
We're giving away four copies of liveProject: Build an ML Recommender System and have Kim Falk on-line!
See this thread for details.
Win a copy of liveProject: Build an ML Recommender System this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

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

 
Ranch Hand
Posts: 436
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The solution that ended up working for me was



Code on, - Dave
 
Mark Spritzler
ranger
Posts: 17346
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic