MockMvc中测试产生的问题org.springframework.web.util.NestedServletException
通过Mock测试Spring MVC发生的问题:
@Test public void shouldProcessRegistration() throws Exception{ SpitterRepository mockReopsitory = Mockito.mock(SpitterRepository.class); //通过Mock生成一个虚拟的对象实例SpitterRepository是一个接口且没有实例化,只能虚拟 Spitter unsaved = new Spitter("jbauer","24hours","Jack","Bauer"); //新键了两个实例 Spitter saved=new Spitter(24L,"jbauer","24hours","Jack","Bauer"); Mockito.when(mockReopsitory.save(unsaved)).thenReturn(saved); //当mockReopsitory存储UNsaved之后返回saved对象 SpitterController controller = new SpitterController(mockReopsitory);//传入mockReopsitory,因为SpitterController包含这个成员变量的 MockMvc mockMvc = MockMvcBuilders.standaloneSetup(controller).build();//单独建立controller这个虚拟MVC
//在构建完SpitterRepository的mock实现以及所要执行的控制器和MockMvc之后,shouldProcess-Registration()对“/spitter/register”发起了一个POST请求。
//作为请求的一部分,用户信息以参数的形式放到request中,从而模拟提交的表单。 mockMvc.perform(MockMvcRequestBuilders.post("/spitter/register") .param("firstName", "Jack") .param("lastName", "Bauer") .param("username", "jbauer") .param("password", "24hours")) .andExpect(MockMvcResultMatchers.redirectedUrl("/spitter/jbauer")); Mockito.verify(mockReopsitory, Mockito.atLeastOnce()).save(unsaved); //测试会校验SpitterRepository的mock实现最终会真正用来保存表单上传入的数据。 } }
@RequestMapping(value="/register",method=RequestMethod.POST) public String processRegistration(Spitter spitter){ spitterRepository.save(spitter); return "redirect:/spitter/" +spitter.getUsername(); }
错误一:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [spittr.Spitter]: No default constructor found; nested exception is java.lang.NoSuchMethodException: spittr.Spitter.<init>()
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:973)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
at org.springframework.test.web.servlet.TestDispatcherServlet.service(TestDispatcherServlet.java:64)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.springframework.mock.web.MockFilterChain$ServletFilterProxy.doFilter(MockFilterChain.java:170)
at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:137)
at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:141)
at spittr.test.HomeControllerTest.shouldProcessRegistration(HomeControllerTest.java:109)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [spittr.Spitter]: No default constructor found; nested exception is java.lang.NoSuchMethodException: spittr.Spitter.<init>()
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:108)
at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.createAttribute(ModelAttributeMethodProcessor.java:138)
at org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.createAttribute(ServletModelAttributeMethodProcessor.java:81)
at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:104)
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:79)
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:157)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:124)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:690)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
... 32 more
Caused by: java.lang.NoSuchMethodException: spittr.Spitter.<init>()
at java.lang.Class.getConstructor0(Class.java:2810)
at java.lang.Class.getDeclaredConstructor(Class.java:2053)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:105)
... 45 more
原因:No default constructor found;
由于mockMvc.perform(MockMvcRequestBuilders.post("/spitter/register"),要调用无参的构造函数构造对象。
当Spitter类中的没有定义无参的构造函数,报错。
错误二:
java.lang.AssertionError: Redirected URL expected:</spitter/jbauer> but was:</spitter/null>
at org.springframework.test.util.AssertionErrors.fail(AssertionErrors.java:60)
at org.springframework.test.util.AssertionErrors.assertEquals(AssertionErrors.java:89)
at org.springframework.test.web.servlet.result.MockMvcResultMatchers$3.match(MockMvcResultMatchers.java:127)
at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:149)
at spittr.test.HomeControllerTest.shouldProcessRegistration(HomeControllerTest.java:114)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
原因:andExpect(MockMvcResultMatchers.redirectedUrl("/spitter/jbauer"))
当修改错误的错误之后,产生url错误,原因:在processRegistration的参数时一个对象,当传入参数的是后,是要默认调用参数的setter方法的,
所以,要在Spitter类中添加对应的setter方法。
或者改为:就不用添加setter方法了
@RequestMapping(value="/register",method=RequestMethod.POST) public String processRegistration( @RequestParam("firstName") String firstName, @RequestParam("lastName") String lastName, @RequestParam("username") String username, @RequestParam("password") String password){ spitterRepository.save(new Spitter(firstName, lastName, username, password)); return "redirect:/spitter/" + username; }
错误三:
Argument(s) are different! Wanted:
spitterRepository.save(
spittr.Spitter@fa21b3e
);
-> at spittr.test.HomeControllerTest.shouldProcessRegistration(HomeControllerTest.java:115)
Actual invocation has different arguments:
spitterRepository.save(
spittr.Spitter@5baa8b76
);
-> at spittr.web.SpitterController.processRegistration(SpitterController.java:34)
原因,比较两个对象默认是比较的对象的地址。
需要重写其比较的方法
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
@Override public boolean equals(Object that) { return EqualsBuilder.reflectionEquals(this, that, "firstName", "lastName", "username", "password", "email"); } @Override public int hashCode() { return HashCodeBuilder.reflectionHashCode(this, "firstName", "lastName", "username", "password", "email"); }