Results 1 to 2 of 2

Thread: Base Unit Test for Controllers?

  1. #1
    Join Date
    Aug 2004
    Location
    St. Louis, MO
    Posts
    39

    Default Base Unit Test for Controllers?

    Is there a base class that I should be using for testing controllers (looking in org.springframework.test but I'm not seeing anything geared towards controller testing) or should I be writing my own using XmlWebApplicationContext or some such context? Any other tips pertaining to controller testing would be appreciated, as well.

    Thanks,

    Ryan

  2. #2
    Join Date
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,104

    Default

    Take a look at MockHttpServletRequest and MockHttpServletResponse in spring-mock.jar

    There are a few options for testing. For integration tests, you can use AbstractDependencyInjectionSpringContextTests.

    To test a smaller scope which is often pereferable, you can insert mocks (e.g. using EasyMock) for your services layer.

    Here's an incomplete mock example:
    Code:
            //Add request data
            request = new MockHttpServletRequest("POST", "/");
            response = new MockHttpServletResponse();
    
            request.addParameter("formInputName", "formValue");
            ...
    
            //MockControl.createControl(MyService.class); in setUp
            //Add mock behaviour
            ....
            mc.setThrowable(new DuplicateException("mock"));
            mc.replay();
    
            //Now test controller
            ModelAndView mv = myController.handleRequest(request, response);
            BindException errors = (BindException) mv.getModel().get(BindException.ERROR_KEY_PREFIX+"command");
            
            assertNotNull(errors);
    And don't forget mc.verify() in your teardown.

Similar Threads

  1. verify delete in unit test
    By paul.barry in forum Data
    Replies: 17
    Last Post: Nov 11th, 2006, 03:24 PM
  2. Replies: 2
    Last Post: Sep 28th, 2005, 02:47 PM
  3. Unit Test - Problems using Data Source
    By batistella in forum Container
    Replies: 5
    Last Post: Sep 22nd, 2005, 06:08 PM
  4. Separation of integration and unit test source
    By eliot in forum Architecture
    Replies: 4
    Last Post: Jan 30th, 2005, 01:27 PM
  5. Replies: 1
    Last Post: Sep 30th, 2004, 09:26 AM

Posting Permissions

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