File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Spring and the fly likes Integration testing of Spring 2.5 Annotation based controller using web context Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Integration testing of Spring 2.5 Annotation based controller using web context" Watch "Integration testing of Spring 2.5 Annotation based controller using web context" New topic
Author

Integration testing of Spring 2.5 Annotation based controller using web context

Jignesh Gohel
Ranch Hand

Joined: Dec 28, 2004
Posts: 276
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.


Regards,
Jignesh

The Art Of Life Is To Know When To Be Useless And When To Be Useful - CHUANG TZU
raj londonboy
Greenhorn

Joined: Aug 17, 2011
Posts: 7
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

Joined: Aug 17, 2011
Posts: 7
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
 
subject: Integration testing of Spring 2.5 Annotation based controller using web context
 
Similar Threads
Spring MVC question
Problem with validation example
spring form submission
issues in calling the ejbcontroller (EJB 3.o ) from the controller using spring Frame
Autowiring of fields failed; nested exception is org.springframework.beans.factory.BeanCreationExcep