title: @ModelAttribute controller spring-mvc mocking
body: <p>I want to test a controller which is using <code>@ModelAttribute</code> for one of its method arguments.</p>
  
  <pre><code>public String processSaveAction(@ModelAttribute("exampleEntity") ExampleEntity exampleEntity)
  </code></pre>
  
  <p><code>@ModelAttribute</code> method <code>getExampleEntity</code> is using <code>@RequestParam</code>:</p>
  
  <pre><code>@ModelAttribute("exampleEntity")
  public ExampleEntity getExampleEntity(@RequestParam(value = "id", required = true) ExampleEntity exampleEntity) {
  </code></pre>
  
  <p>My controller is using <code>WebDataBinder</code> to call a factory, which returns an object based on param "id".</p>
  
  <pre><code>@Controller
  public class ExampleController(){
  
      @Autowired private IdEditorFactory idEditorFactory;
  
      @InitBinder
      public void initBinder(WebDataBinder binder) {
  
          binder.registerCustomEditor(ExampleEntity.class, idEditorFactory.createEditor(ExampleEntity.class));
      }
  
      @ModelAttribute("exampleEntity")
      public ExampleEntity getExampleEntity(@RequestParam(value = "id", required = true) ExampleEntity exampleEntity) {
  
          //Irrelevant operations
          return exampleEntity;
      }
  
      @RequestMapping(method = RequestMethod.POST, params = "action=save")
      public String processSaveAction(
              @RequestParam(value = "confirmed") String exampleString,
              @ModelAttribute("exampleEntity") ExampleEntity exampleEntity,
              BindingResult result, HttpServletRequest request)
              throws IOException {
  
          boolean success = editorProcessor.processSaveAction(exampleString,
                  exampleEntity, result, request);
  
          return success ? getSuccessView(exampleEntity) : VIEW_NAME;
      }
  }
  </code></pre>
  
  <p>And my test:</p>
  
  <pre><code>@WebAppConfiguration
  public class ExampleControllerTest{
  
      @Mock private EditorProcessor editorProcessor;
      @Mock private IdEditorFactory idEditorFactory;
      @InjectMocks private ExampleController exampleController;
  
      private MockMvc mockMvc;
  
  
      @Before
      public void setUp() throws Exception {
  
          MockitoAnnotations.initMocks(this);
          mockMvc = MockMvcBuilders.standaloneSetup(exampleController).build();
  
          WebDataBinder webDataBinder = new WebDataBinder(ExampleEntity.class);
          webDataBinder.registerCustomEditor(ExampleEntity.class, idEditorFactory.createEditor(ExampleEntity.class));
      }
  
      @Test
      public void shouldProcessSaveAction() throws Exception {
  
          // given
          BindingResult result = mock(BindingResult.class);
          ExampleEntity exampleEntity = mock(ExampleEntity.class);
          HttpServletRequest httpServletRequest = mock(HttpServletRequest.class);
  
          given(editorProcessor.processSaveAction("confirmed", exampleEntity, result, httpServletRequest)).willReturn(true);
  
          // when
          ResultActions perform = mockMvc.perform(post("/").sessionAttr("exampleEntity", exampleEntity)
                                                              .param("id", "123456"
                                                              .param("action","save"));
  
          // then
          perform.andDo(print())
                  .andExpect(status().isOk());
  
      }
  }
  </code></pre>
  
  <p>I want to somehow mock <code>getExampleEntity()</code> so that every time I perform a POST with parameter "id", I receive a mocked object ("exampleEntity") for the test.</p>
  
  <p>I could introduce <code>@Binding</code> to the test, but then I would have to mock many levels of methods (like initBinder -> idEditoryFactory-> editor -> hibernateTemplate and so on) only to get an entity from some source (for example, a database).</p>