Results 1 to 4 of 4

Thread: MockTest and post method

Hybrid View

  1. #1

    Default MockTest and post method

    Here is my code, the pb is that the view return is null ... an idea ? :
    Code:
    fillUtilisateur();
    LoginFormController classe = new LoginFormController(  );
    
    String formView = "fail";
    String successView = "success";
    classe.setFormView( formView );
    classe.setSuccessView( successView );
    classe.setCommandName("utilisateur");
    classe.setCommandClass(utilisateur.class);
            
    BindException errors = new BindException(utilisateur, "utilisateur");
    MockHttpServletRequest 	request=new MockHttpServletRequest("POST", "/login.html");
    HttpServletResponse 	response = new MockHttpServletResponse();
    request.addParameter("login", utilisateur.getLogin().toString());
    request.addParameter("mdp", utilisateur.getMdp().toString());
    mv = classe.onSubmit(request, response, utilisateur, errors);
    Thanks a lot,

    Fabien

  2. #2
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    Fabien, could you show an excerpt from your LoginFormController. This will help greatly.
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  3. #3
    Join Date
    Aug 2004
    Location
    Amsterdam, Netherlands
    Posts
    450

    Default

    What you're doing here is testing a part of the form controller. You should execute the entire workflow of the formcontroller, which basically begins with handling the request using the handleRequest(HttpServletRequest, HttpServletResponse) method. This returns a ModelAndView object which should contain the view you want to have.

    So again: test the entire workflow and not just the onSubmit method. Binding of parameters to the command object hasn't been carried out yet when you call the onSubmit method directly so any logic in your controller that assumes binding has already occurred will not function correctly.

    Alef

  4. #4

    Default

    Thanks a lot, here is what i finally write it's work fine now

    LoginControllerTest

    Code:
    package org.astre.sig.web;
    
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.astre.sig.modele.Utilisateur;
    import org.astre.sig.service.UtilisateursManager;
    import org.springframework.mock.web.MockHttpServletRequest;
    import org.springframework.mock.web.MockHttpServletResponse;
    import org.springframework.web.servlet.ModelAndView;
    
    public class LoginControllerTest extends BaseControllerTest {
    
    	private static Log log = LogFactory.getLog(LoginControllerTest.class);
    	
    	private UtilisateursManager 	mgrUtilisateurs;
    	private Utilisateur utilisateur;
        private MockHttpServletRequest request;
    	
        private LoginFormController c;
        private ModelAndView mv;
    		
    	public LoginControllerTest(String nomTest){
    		super(nomTest);		
    	}
    
    	private void fillUtilisateur(){
    		utilisateur=new Utilisateur();		
    		utilisateur.setId(new Long(0));
    		utilisateur.setLogin("testlog");
    		utilisateur.setMdp("secret");
    		mgrUtilisateurs.saveUtilisateur(utilisateur);
    	}
    
    	
    	public void testLoginOk() throws Exception {
            log.debug("testing LoginOk...");
            fillUtilisateur();
            request = newPost("/login.html");
    		request.addParameter("login", utilisateur.getLogin().toString());
    		request.addParameter("mdp", utilisateur.getMdp().toString());
    
            mv = c.handleRequest(request, new MockHttpServletResponse());
    
            log.info(mv.getViewName());
    		assertEquals("accueil", mv.getViewName());
    	} 
    
    	public void testLoginKo() throws Exception {
            log.debug("testing LoginKo...");
            fillUtilisateur();
            utilisateur.setMdp("Mauvais mot de passe");
            request = newPost("/login.html");
    		request.addParameter("login", utilisateur.getLogin().toString());
    		request.addParameter("mdp", utilisateur.getMdp().toString());
    
            mv = c.handleRequest(request, new MockHttpServletResponse());
    
            assertNotNull("Attention aucun message d'erreur de login renvoyé", request.getSession().getAttribute("errors"));
            assertEquals("loginForm", mv.getViewName());
    	} 
    
    	public void setUp() throws Exception {
    		c=(LoginFormController)ctx.getBean("loginFormController");
    		mgrUtilisateurs=(UtilisateursManager)ctx.getBean("utilisateursManager");		
    	}
    
    	public void tearDown() throws Exception {
    		super.tearDown();
    	}
    	
    	public static void main(String[] args) {
    		junit.textui.TestRunner.run(LoginControllerTest.class);
    	}	
    }
    And the BaseControllerTest :


    Code:
    package org.astre.sig.web;
    
    import junit.framework.TestCase;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.mock.web.MockHttpServletRequest;
    
    public class BaseControllerTest extends TestCase {
    
    	public static ApplicationContext ctx;
    	
    	static {
    		String[] paths = {"/WEB-INF/applicationContext.xml", "/WEB-INF/action-servlet.xml"};
    		ctx=new ClassPathXmlApplicationContext(paths);
    	}
    	
        public MockHttpServletRequest newPost(String url) {
            return new MockHttpServletRequest(null, "POST", url);   
        }
    
        public MockHttpServletRequest newGet(String url) {
            return new MockHttpServletRequest(null, "GET", url);   
        }	
    	
    	public BaseControllerTest(String nomTest) {
    		super(nomTest);
    	}
    	
    	public void test(){		
    	}
    
    	public void setUp() throws Exception {
    		super.setUp();
    	}
    
    	public void tearDown() throws Exception {
    		super.tearDown();
    	}
    
    }
    Thanks again !

Posting Permissions

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