• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

"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: 17347
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: 17347
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic