Announcement Announcement Module
Collapse
No announcement yet.
JUnit testing Controller referenceData Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • JUnit testing Controller referenceData

    I have a basic working Spring MVC application, and now I am adding test cases.
    So, I have one controller I am using, and I have the test-context.xml file.
    This is based on the Step-by-Step example.

    My controller uses referenceData to get data from the database before the page is shown, so I have a drop-down list. I am writing a test case for this controller and I thought it would be appropriate to test referenceData ... but exactly how do I test this?

    The step-by-step InventoryController has a method for: handleRequest
    and a test method called: testHandleRequestView
    Likewise, my controller method called: referenceData
    So, would the test method me: testReferenceDataView???

    Or would I make a mock request (or mock servlet request) to my page, and look to see if I got something back in that Map?

    I'm a newbie to Spring MVC, and just as new to Spring MVC unit testing.

    Thanks for any help!
    Tom

  • #2
    Hi Tom,

    To be perfectly honest with you, if this is a new project you're working on, I would not even look at the outdated step-by-step tutorial for Spring MVC. That tutorial is based on the now (as of Spring 3.0) deprecated abstract base controller hierarchy.

    Going forward (i.e., from Spring 2.5 onward) we recommend that you use the new annotation-driven MVC framework. See the Spring Reference Manual for details on @Controller, @RequestMapping, @RequestParam, @ModelAttribute, etc.

    The annotation-driven approach will lead to code which is much easier to unit test. Using your example, you could unit test the referenceData() method by calling it directly (i.e., without any mocks or stubs).

    Originally posted by Tom Holmes View Post
    My controller uses referenceData to get data from the database before the page is shown, so I have a drop-down list. I am writing a test case for this controller and I thought it would be appropriate to test referenceData ... but exactly how do I test this?
    If you truly need to base your application on the old controller hierarchy, then... yes, you could create a testReferenceData() method based on the testHandleRequestView() method and pass in a MockHttpServletRequest. You could then verify that the expected attributes were set in the returned Map.

    Regards,

    Sam

    Comment

    Working...
    X