Page 2 of 2 FirstFirst 12
Results 11 to 17 of 17

Thread: Common Model Objects

  1. #11
    Join Date
    May 2007
    Posts
    10

    Default

    Well, essentially I wanted to create what spring calls interceptor which is what you just helped with. Any time a handleRequest method was called on a controller I wanted to intercept the call, add stuff the model, then invoke the method.

    Now here's here another question for you, regarding unit testing. How do I get test my RequestHandler in a full test? When I do my normal Spring testing, the dispatcher servlet isnt loaded, hence the interceptors arent listening. I have tried loading it a bunch of ways but none seem to work. Thoughts?

  2. #12
    Join Date
    Jul 2005
    Location
    Idaho
    Posts
    231

    Default

    John,

    I'm not sure how you are setting up your tests, but I usually do something like:
    Code:
    public void setUp() {
      String[] paths = 
      { "/WEB-INF/ServletToTest-servlet.xml", };
        ctx = new XmlWebApplicationContext();
        ctx.setConfigLocations( paths );
        ctx.setServletContext( new MockServletContext( "" ) );
        ctx.refresh();
      }
    }
    Then in the test, do something like:
    Code:
    public void testTheEvilBreakingCode () throws Exception {
      ContToTestController cont = ( ContToTestController ) ctx.getBean( "contToTestController" );
      ... run some tests
    }
    Then, the fun begins.

    Hope that helps... if not, I would be curious to see how you are setting up your tests.

    Steve O

  3. #13
    Join Date
    May 2007
    Posts
    10

    Default

    Steve O,

    I did try that but the interceptors are never invoked. Most likely because it is somewhere else in the stack.

  4. #14
    Join Date
    Apr 2007
    Location
    Wellington, New Zealand
    Posts
    125

    Default

    Hi John,

    The interceptors are used by the DispatcherServlet and thus can not be tested with how you are proposing to as the test requests are not going through a DispatcherServlet but straight to the controller.

    I wouldn't worry about testing the interceptor working with the controller, I would test each individually for what they do.

    Josh

  5. #15
    Join Date
    May 2007
    Posts
    10

    Default

    I had a feeling you were gonna say that. I got that to work but I really wanted an integration test in its entirety. That seems odd to me, no? I have become so accustomed to programatic wiring and the ability to test processes as a whole.

    Again, thanks for all the help.

  6. #16
    Join Date
    Apr 2007
    Location
    Wellington, New Zealand
    Posts
    125

    Default

    Hey John,

    But if you think about it, they are two very different things to test. The interceptor is just adding information to the model-and-view based on the view type, while the controller is actually preforming the logic for the page requested.

    I am sure you could try to create a DispatcherServlet and run a controller test, but it sounds like more hassel than it would be worth.

    Josh

  7. #17
    Join Date
    May 2008
    Posts
    10

    Default Redirect querystring has model info

    Quote Originally Posted by joshk View Post
    Be warned, if you add common model data and then redirect, the model data will be appended to the query string
    Here's another way to prevent model information from being appended to the querystring upon "redirect:". It's a workaround. Don't issue the Spring "redirect:" - use response.sendRedirect() instead.

Posting Permissions

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