Results 1 to 7 of 7

Thread: Unit Testing using MVC and Annotations

  1. #1

    Default 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. #2
    Join Date
    Sep 2008
    Posts
    5

    Question 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

  3. #3

    Default

    up, me to

  4. #4
    Join Date
    Sep 2008
    Location
    London, UK
    Posts
    155

    Default

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

    Cheers

    G
    Attached Files Attached Files

  5. #5
    Join Date
    May 2008
    Location
    Central Florida, U.S.A.
    Posts
    36

    Default

    @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 at 11:04 AM. Reason: Mispellings

  6. #6

    Default

    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.

  7. #7
    Join Date
    Aug 2006
    Location
    Brooklyn
    Posts
    556

    Default

    Quote 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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •