Announcement Announcement Module
Collapse
No announcement yet.
TDD newbie Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • TDD newbie

    Hi all

    I'm currently exploring the recommended "test first" approach in Spring development, and I must admit I'm fascinated by all the possibilities theis method has.

    But I'm also a newbie in this way of thinking, and have run into problems

    I wonder why

    Code:
    private MockControl control = null;
        private LogicFacade mockLogic = null;
        private DateSearchFormController datefc = null;
        private MockHttpServletRequest mockreq = null;
        
    
        protected void setUp() throws Exception {
            control = MockControl.createControl(LogicFacade.class);
            mockLogic = (LogicFacade) control.getMock();
            datefc = new DateSearchFormController();
            datefc.setLogic(mockLogic);
            mockreq = new MockHttpServletRequest();
        }
        
        protected void tearDown() throws Exception {
            control = null;
            mockLogic = null;
            datefc = null;
            mockreq = null;
        }
        
        public void testLoginCheck() throws Exception {
        	MockHttpSession mockSession = new MockHttpSession();
        	MockControl sessionControl = MockControl.createControl(UserSession.class);
        	UserSession session = (UserSession) sessionControl.getMock();
        	
        	AvikUser user = new AvikUser();
        	user.setRole(AvikRole.POWERUSER);
        	session.setUser(user);
        	
        	mockSession.setAttribute("usersession", session);
        	mockreq.setAttribute("something", new Object());
        	mockreq.setSession(mockSession);
        	datefc.handleRequest( mockreq , new MockHttpServletResponse() );
        	
        }
    generates the following exception

    Code:
        [junit] Testcase: testLoginCheck(ks.rah.avik2.test.TestSearchController):   Caused an ERROR
        [junit] Request method 'null' not supported
        [junit] org.springframework.web.servlet.support.RequestMethodNotSupportedException: Request method 'null' not suppor
    ted
        [junit]     at org.springframework.web.servlet.support.WebContentGenerator.checkAndPrepare(WebContentGenerator.java:
    199)
        [junit]     at org.springframework.web.servlet.support.WebContentGenerator.checkAndPrepare(WebContentGenerator.java:
    178)
        [junit]     at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:109)
        [junit]     at ks.rah.avik2.test.TestSearchController.testLoginCheck(TestSearchController.java:61)
        [junit]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        [junit]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        [junit]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    testing this code


    Code:
    protected ModelAndView onSubmit(HttpServletRequest request, 
    									HttpServletResponse response,
    									Object command, 
    									BindException errors) throws Exception {
    		DateSearchForm form = (DateSearchForm) command;
    		AvikUser user = AvikController.checkLoggedIn(request, AvikRole.POWERUSER, false);
    .
    .
    .
    Seems like the MockHttpServletRequest/Response is null somehow.....

    Anybody have an idea?

  • #2
    You are using the constructor for MockHttpServletRequest which does not specify any 'method' string ('GET' or 'POST'), so you need to set this yourself manually as a property.

    Comment


    • #3
      That did it.

      Thanks!

      Comment

      Working...
      X