Announcement Announcement Module
Collapse
No announcement yet.
Unit Testing using MVC and Annotations Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Unit Testing using MVC and Annotations

    Hi all,

    I am currently using the Spring Web MVC in a project. This is my first project using this technology and I am responsible for unit and integration testing. I am confused about how to test my Controllers when I use the annotation stuff.

    There is a controller called UserController. (Sorry I cant use the AT-sign in the forum, yet)
    Code:
    ATController
    ATSessionAttributes("user")
    public class UserController{
    
    private final UserService userService;
    
        ATAutowired
        public UserController(UserService userService) {
            this.userService = userService;
        }
    
    ATRequestMapping(value = "create", method = RequestMethod.GET)
        public ModelMap createGet() throws Exception { 
            return (new ModelMap()).addAttribute("user", new User());
       }
    ...
    }
    There are also some more methods. Now I want to write a test case for the function createGet.

    Code:
    public class UserServiceTest{
    ATTest
    public void testmethod(){
     //Test me
    }
    }
    My problem is, I don't know how to implement my tests. In some examples people called a method called controller.handleRequest(..) but where is no such method in my case. My Controller does not extend any class where it
    may get this method (maybe injected at runtime because ATController annotation).

    I am sure that I can test the method createGet() directly, for example calling it and examining the received ModelMap but how can I check that the rest of the ModelAndView did NOT change? I only got the ModelMap back but I don't know the actual ModelAndView. I am confused Are there any good tutorials that cover this? I only found trivial examples.

    Cheers,
    Daniel (a bloody spring noob)

  • #2
    I also want to know that

    Hi Springs,

    I also want to know how to write jnuit case for new annotated controller.

    Any hint highly appreciates.

    Thanks
    Cham

    Comment


    • #3
      up, me to

      Comment


      • #4
        I can contribute with a few integration tests, using in-memory db.

        Cheers

        G

        Comment


        • #5
          @Daniel, it appears that you don't want to just check the method response but also the integration with Spring MVC, correct? If so, then you will need to take a look at:
          Code:
          org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter#handle(HttpServletRequest request, HttpServletResponse response, Object handler)
          This is the adapter that knows how to handle the controller POJO. It should give you ideas on how and what you want to test. This class is not well documented, but it is fairly easy to follow in the code.

          If you aren't concerned with integration into Spring MVC, then you can simply test the method to be sure the ModelMap is as expected. The annotations just determine what should be injected, so you can mock those approriately
          Last edited by mrcritical; Dec 23rd, 2008, 11:04 AM. Reason: Mispellings

          Comment


          • #6
            thank you for your reply,

            in the same way, do you now how i can just test annotated controller,

            for wexample :
            i want to test that request("/index.do") return the good ModelAndView.

            Comment


            • #7
              Originally posted by nullnullschneider View Post

              I am sure that I can test the method createGet() directly, for example calling it and examining the received ModelMap but how can I check that the rest of the ModelAndView did NOT change? I only got the ModelMap back but I don't know the actual ModelAndView. I am confused Are there any good tutorials that cover this? I only found trivial examples.

              Cheers,
              Daniel (a bloody spring noob)
              When unit-testing the controller that's really all you want to do. Incidentally prefer taking a Model as input over returning it. It saves you the extra line.

              One option for end-to-end tests is to use a tool like Selenium. That will make sure your entire request works including Spring MVC configuration.

              If you just want to write unit tests to experiment with @MVC annotations, you can look up how the Spring Framework does it. Check out from source and look at the unit tests.

              Comment

              Working...
              X