File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Integration testing of Spring 2.5 Annotation based controller using web context

 
Jignesh Gohel
Ranch Hand
Posts: 276
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Custom Web Context Loader



Base Test Class to be inherited by the test controllers



Controller



Test Controller



spring-servlet.xml



Actually I dont have the presentation tier(JSP Pages) ready.And I want to test my controller to verify that it is behaving correctly.
As can be seen in controller the YGUser model object will be populated from the values in the html form(will be generated by the spring form tag library) in the JSP page.

In such a case I will receive a correctly populated YGUser instance in the controller when actually hitting the request
"..../registerUser.do"

I dont want to use @RequestParam annotation for the method argument YGUser.

When debugging the test controller, I am receiving a YGUser instance but with the instance properties values as null because of the @ModelAttribute annotated method being called first.

I am testing the Spring 2.5 annotation based controllers first time.Above is what I could find on the web for doing the same.

I am stuck at how to proceed ahead, in the sense that how to provide the appropriate populated model objects to MockHttpServletRequest
and verify that I am getting the expected response, the view, etc?

For e.g. in my testRegisterUser() method, I have created a "testUser" instance (which in real scenario would be created by Spring implicitly
because of the YGUser model attribute bound to spring's form tag in the JSP page).

But how to pass the same to my test context so that actually when the registerUser() method in the controller is called the YGUser is populated with the test values I set in the test method.

Similarly If I want to validate my YGUser object before saving to the database using Spring Validation API then how to test it in the context mentioned above.

Please help in making it work.
Also, any comments on incorrect usage of API or improving the code using API in an efficient way would be really helpful.

 
raj londonboy
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jignesh,

I am running in the same problem now. have you got any resolution for the same??

It will be great if you can share the solution if you got any?

Cheers,
Raj
 
raj londonboy
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have got all this sorted now. I was sending mock response null which was leading to test failures. Its working for me now.

Working Context Loader:




 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic