Results 1 to 3 of 3

Thread: testing with jmock

  1. #1
    Join Date
    Jan 2007
    Posts
    155

    Cool testing with jmock

    i want to test this method..

    Code:
    @Override
    	protected ModelAndView onSubmit(HttpServletRequest request,HttpServletResponse response,Object command,BindException errors) throws Exception {
            HashMap<String,String> model = new HashMap<String,String>();
            UnidadCompuesta uc = (UnidadCompuesta) command;
    		
            
    		try{
    			servicioUnidad.crearUnidad(uc);
    		}catch(UnidadNoCreada e){
    			errors.rejectValue("nombre","unidad.noCreada"); 
    		}
    
    		model.put("unidadCreadaNombre",uc.getNombre());
            model.put("unidadCreadaEmail",uc.getEmail());
            
    		return new ModelAndView(getSuccessView(),model);
    	}
    i write this test:

    Code:
    package test.unitarios;
    
    import org.jmock.Mock;
    import org.jmock.MockObjectTestCase;
    import org.springframework.mock.web.MockHttpServletRequest;
    import org.springframework.mock.web.MockHttpServletResponse;
    
    import com.dominio.exceptions.UnidadNoCreada;
    import com.dominio.unidades.UnidadCompuesta;
    import com.servicios.ServicioUnidad;
    import com.web.CrearUnidadController;
    
    public class CrearUnidadControllerTest extends MockObjectTestCase {
    
    	//Controlador a usar en los unit-test
    	private CrearUnidadController controller;
    	//Mock de servicioUnidad
    	private Mock mockServicioUnidad;
    	//Servicio ausar en los unit-test
    	private ServicioUnidad servicioUnidad;
    	//Mock para el request
    	private MockHttpServletRequest  request;
    	//Mock para el response
    	private MockHttpServletResponse response;
    	
    	/**
    	 * Metodo para configurar el mock de servicioUnidad
    	 *  {@link com.servicios.ServicioUnidad}
    	 * @see junit.framework.TestCase#setUp()
    	 */ 
    	protected void setUp() throws Exception {
    		super.setUp();
    		controller = new CrearUnidadController();
    		mockServicioUnidad = mock(ServicioUnidad.class);
    		servicioUnidad = (ServicioUnidad)mockServicioUnidad.proxy();
    		
    		request = new MockHttpServletRequest();
    		response = new MockHttpServletResponse();
    	}
    
    	public void testOnSubmitCrearUnidadConException() throws Exception{
    		request.setMethod("POST");
    		request.setParameter("nombre","Facultad de ingenieria");
    		request.setParameter("email","FI@fi.uba.ar");
    
    		UnidadCompuesta uc = new UnidadCompuesta();
    		uc.setNombre("Facultad de ingeneria");
    		uc.setEmail("FI@fi.uba.ar");
    		
    		mockServicioUnidad.expects(once())
    				.method("crearUnidad")
    				.with(same(uc))
    				.will(throwException(new UnidadNoCreada("Unidad no creada")));
    		
    		controller.handleRequest(request,response);
    			//deberia ver si seteo los errores
    			
    		mockServicioUnidad.verify();
    
    	}
    
    }
    and i have this error:
    Code:
    java.lang.NullPointerException
    	at com.web.CrearUnidadController.onSubmit(CrearUnidadController.java:53)
    	at org.springframework.web.servlet.mvc.SimpleFormController.processFormSubmission(SimpleFormController.java:258)
    	at org.springframework.web.servlet.mvc.AbstractFormController.handleRequestInternal(AbstractFormController.java:250)
    	at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)
    	at test.unitarios.CrearUnidadControllerTest.testOnSubmitCrearUnidadConException(CrearUnidadControllerTest.java:89)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    	at java.lang.reflect.Method.invoke(Method.java:585)
    	at junit.framework.TestCase.runTest(TestCase.java:154)
    	at org.jmock.core.VerifyingTestCase.runBare(VerifyingTestCase.java:39)
    	at junit.framework.TestResult$1.protect(TestResult.java:106)
    	at junit.framework.TestResult.runProtected(TestResult.java:124)
    	at junit.framework.TestResult.run(TestResult.java:109)
    	at junit.framework.TestCase.run(TestCase.java:118)
    	at junit.framework.TestSuite.runTest(TestSuite.java:208)
    	at junit.framework.TestSuite.run(TestSuite.java:203)
    	at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
    	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
    .. i cant understand this jmock library ... any idea??

    Thank you!

  2. #2
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    It would be useful to know which line 53 is. Its a little difficult to know the the error is (CrearUnidadController.java:53). I'm guessing that it might be the call to ServicioUnidad....

  3. #3
    Join Date
    Jan 2007
    Posts
    155

    Default jmock

    As always your advice is excellent!
    .. i forgot to set the service object into the controller, here is the
    right code

    Thank you for your help!

    Code:
    package test.unitarios;
    
    import org.jmock.Mock;
    import org.jmock.MockObjectTestCase;
    import org.springframework.mock.web.MockHttpServletRequest;
    import org.springframework.mock.web.MockHttpServletResponse;
    import org.springframework.web.servlet.ModelAndView;
    
    import com.dao.UnidadDao;
    import com.dominio.exceptions.UnidadNoCreada;
    import com.dominio.unidades.UnidadCompuesta;
    import com.servicios.ServicioUnidad;
    import com.web.CrearUnidadController;
    
    public class CrearUnidadControllerTest extends MockObjectTestCase {
    
    	//Controlador a usar en los unit-test
    	private CrearUnidadController controller;
    	//Mock de servicioUnidad
    	private Mock mockServicioUnidad;
    	//Servicio ausar en los unit-test
    	private ServicioUnidad servicioUnidad;
    	//Mock para el request
    	private MockHttpServletRequest  request;
    	//Mock para el response
    	private MockHttpServletResponse response;
    	
    	private ModelAndView mav;
    	
    	
    protected void setUp() throws Exception {
    		super.setUp();
    		controller = new CrearUnidadController();
    		mockServicioUnidad = mock(ServicioUnidad.class);
    		servicioUnidad = (ServicioUnidad)mockServicioUnidad.proxy();
    		controller.setServicioUnidad(servicioUnidad);
    		request = new MockHttpServletRequest();
    		response = new MockHttpServletResponse();
    	}
    
    	
    public void testOnSubmitCrearUnidadSinException() throws Exception{
    		request.setMethod("POST");
    		request.setParameter("nombre","Facultad de ingenieria");
    		request.setParameter("email","FI@fi.uba.ar");
    
    		UnidadCompuesta uc = new UnidadCompuesta();
    		uc.setNombre("Facultad de ingenieria");
    		uc.setEmail("FI@fi.uba.ar");
    		
    		mockServicioUnidad.expects(once())
    				.method("crearUnidad")
    				.with(eq(uc))
    				.will(returnValue(uc));
    				
    		
    		try{
    			mav = controller.handleRequest(request,response);
    		}catch(UnidadNoCreada e){
    			fail("No deberia haber lanzado la exception UnidadNoCreada");
    		}
    		
    		assertEquals(mav.getViewName(),"redirect:/app/nuevaUnidadExito");
    		assertEquals(mav.getModelMap().size(),2);
    		assertEquals(mav.getModel().get("unidadCreadaNombre"),"Facultad de ingenieria");
    		assertEquals(mav.getModel().get("unidadCreadaEmail"),"FI@fi.uba.ar");
    		//OK
    		mockServicioUnidad.verify();
    	}
    }

Posting Permissions

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