Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I give request parameters for a POST using MockMvc

I try to junit the following controller

  @RequestMapping(value="actions.htm", params="reqType=delete",method=RequestMethod.POST)
  @ResponseBody
  public String deletePendingAction(@RequestParam("aPk") Long aPk)
  {
    pendingActionsService.deletePendingAction(aPk);
    return "Deleted";
  }

I use params="reqType=delete" and this is I think the reason why junit fails to map to the controller. I tested all other controllers and they work fine without params tag to the controller. My junit config is:

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = ApplicationConfig.class,loader = AnnotationConfigWebContextLoader.class)
@TransactionConfiguration(defaultRollback = true)
@Transactional
public class JUnitTests {

  @Autowired
  private WebApplicationContext wac;

  private MockMvc mockMvc;

   @Before
   public void SetupContext()
   {
     this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
   }

   @Test
   public void testController() throws Exception
   {
      this.mockMvc.perform(post("/actions.htm","reqType=delete").param("aPk","2"));
   }
}

How do I translate this params tag to the spring mvc junit? Thank you

like image 815
george Avatar asked Aug 27 '15 09:08

george


1 Answers

add param `reqType=delete' to url.

this.mockMvc.perform(post("/actions.htm?reqType=delete")
                .param("aPk", "2")).andReturn().getResponse().getStatus());
like image 165
Nikolay Rusev Avatar answered Nov 15 '22 00:11

Nikolay Rusev