title: MockMvc on spring mvc controller
body: <p>I have the following code in my controller:</p>
  
  <pre><code>@Controller
  @RequestMapping(value="/my")
  public class MyController  {
  
      @RequestMapping(value="/zone/{id}", method=RequestMethod.POST)
      public ModelAndView zone(@PathVariable Long id, @Valid MyFormBean formBean){
  
      System.out.println(formBean.getId());
      ModelMap mod = new ModelMap();
      mod.put("test", true);
      return new ModelAndView(myViewName, model);
      }
  }
  </code></pre>
  
  <p>And the following code in my MyControllerTest class</p>
  
  <pre><code>public class MyControllerTest {
    @InjectMocks
    private MyController myController = new MyController();
  
    private MockMvc mockMvc = MockMvcBuilders.standaloneSetup(MyController).build();
  
    @Before
    public void setUp() {
      MockitoAnnotations.initMocks(this);
    }
  
    @Test
    public void myTest() {
       MyFormBean formBean = new MyFormBean();
       formBean.setId(5L);
       ModelAndView mav = new ModelAndView();
  
       int id = 10;
  
       try {
         MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post("/my/zone/" + id).param("formData", formData.toString())).andReturn();
       } catch(Exception e) {
         fail(e.getMessage());
       }
  }
  </code></pre>
  
  <p>The id field in MyFormBean is annotated with @NotNull</p>
  
  <p>So I tried various permutations of this (requestAttr, sessionAttr, etc. instead of param, and a few others) and I cannot get it to work. At best I get exceptions on the @Valid clause. Other times the test will pass without actually entering the method.</p>
  
  <p>I have tried to follow a few examples but can't find anything with my specific method signature.</p>
  
  <p>Any help appreciated, thanks.</p>