2013-10-09 18 views
7

Ho scritto questo metodo:? CodiceCome verificare che il metodo ha aggiunto redirectAttributes (da MockMvc)

@RequestMapping(value="/someURL", method=GET) 
public String myMethod(RedirectAttributes redirectAttributes) 
{ 
    redirectAttributes.addAttribute("rd", "rdValue"); 
    redirectAttributes.addFlashAttribute("fa", faValue); 
    return "redirect:/someOtherURL"; 
} 

prova:

@Test 
    public void myMethod() throws Exception{ 

    MockHttpServletRequestBuilder request = MockMvcRequestBuilders 
       .get("/someURL"); 
    ResultActions result = mockMvc.perform(request); 
} 

Se invoco esecuzione del test vedo:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.web.bind.annotation.support.HandlerMethodInvocationException: Failed to invoke handler method [public java.lang.String com.epam.hhsystem.web.controllers.VacancyMenuController.myMethod(org.springframework.web.servlet.mvc.support.RedirectAttributes)]; nested exception is java.lang.IllegalStateException: Argument [RedirectAttributes] is of type Model or Map but is not assignable from the actual model. You may need to switch newer MVC infrastructure classes to use this argument. 
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:948) 
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:827) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:690) 
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:812) 
    at org.springframework.test.web.servlet.TestDispatcherServlet.service(TestDispatcherServlet.java:66) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:803) 
    at org.springframework.mock.web.MockFilterChain$ServletFilterProxy.doFilter(MockFilterChain.java:168) 
    at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:136) 
    at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:134) 
    at com.epam.hhsystem.web.controllers.VacancyMenuControllerTest.myMethod(VacancyMenuControllerTest.java:179) 
    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:597) 
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45) 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42) 
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) 
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28) 
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74) 
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83) 
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222) 
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) 
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174) 
    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.web.bind.annotation.support.HandlerMethodInvocationException: Failed to invoke handler method [public java.lang.String com.epam.hhsystem.web.controllers.VacancyMenuController.myMethod(org.springframework.web.servlet.mvc.support.RedirectAttributes)]; nested exception is java.lang.IllegalStateException: Argument [RedirectAttributes] is of type Model or Map but is not assignable from the actual model. You may need to switch newer MVC infrastructure classes to use this argument. 
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:181) 
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:440) 
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:428) 
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925) 
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856) 
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:936) 
    ... 38 more 
Caused by: java.lang.IllegalStateException: Argument [RedirectAttributes] is of type Model or Map but is not assignable from the actual model. You may need to switch newer MVC infrastructure classes to use this argument. 
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.resolveHandlerArguments(HandlerMethodInvoker.java:322) 
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:171) 
    ... 43 more 

Capisco quel problema con RedirectAttributes, ma non capisco come risolverlo.

Da MockMvc come testare che redirectAttributes contiene rd e fa?

Io uso 3.2.3.RELEASE versione Spring Framework

risposta

4

È possibile utilizzare MockMvcResultMatchers#flash() che restituisce un oggetto FlashAttributeResultMatchers che ha un metodo di attribute().

mockMvc.perform(MockMvcRequestBuilders.get("/someURL")) 
      .andExpect(MockMvcResultMatchers.model().attribute("rd", "rdValue")) 
      .andExpect(MockMvcResultMatchers.flash().attribute("fa", someValueThatEqualsFaValue)); 

noti che "rd" sarà un attributo Model, non un attributo flash.

+0

@ user2740224 Quale versione di Spring stai usando? 'RedirectAttributes' sono stati aggiunti in 3.1. Inoltre, dovresti modificare la tua domanda con gli errori che ottieni, non si adattano bene ai commenti. –

+0

L'argomento è stato modificato. Uso la versione con struttura a molla 3.2.3.RELEASE –

Problemi correlati