i want to test this method..
i write this test: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); }
and i have this error: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(); } }
.. i cant understand this jmock library ... any idea??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)
Thank you!


Reply With Quote