2
votes

I know that there are several questions on this .. but still I cannot make it work. I have a spring application for which I'm trying to write integration tests. I try to setup the application context using an xml file (more or less the same xml file that I'm using for the application). The problems that appeared are the following:

  1. The configuration was not loaded and I've received errors like :"expected single bean but found 0."

  2. I've specified the correct path and then it started to complain about resources that are not found on the classpath. I've copied the files that are missing inside the folder test/resources.

  3. Now, it fails to load the application context with this error: No matching bean of type [javax.servlet.http.HttpServletRequest] found for dependency. (In my configuration file I have mvc:annotation-driven />)

  4. I have followed this example http://www.scarba05.co.uk/blog/2010/07/more-on-integration-testing-of-spring%E2%80%99s-mvc-annotation-mapppings-for-controllers/ to make my integration test. Still, when I make the call handlerAdapter.handle(request, response, controller); I receive A ClassCastException because my controller is not an instance of a HandlerAdapter. I almost followed the same error path as the one found here: http://forum.springsource.org/showthread.php?131196-Spring-3-1-2-Controller-annotated-Controllers-JUnit-test-fail.

Yes, I am new to Spring, but at least I've searched and read a lot. Could you please point me to a good tutorial or to some sample projects (other than the spring ones) such that I can solve my problem.

Thank you in advance for the time that you spent on reading my question!

1

1 Answers

3
votes

I have written a blog post on what I think is the best way to override spring application context for testing. Basically, you should not be copying your main application context xml files for testing, as this leads to duplication, instead just override the parts you need to:

However, this doesn't include controller tests. For those, the post you mentioned has now been deprecated by the spring-mvc-test project, which is now included in spring 3.2.x

Here is some reading on spring MVC test framework: