dimanche 30 octobre 2016

org.springframework.web.bind.MissingServletRequestParameterException Spring MVC Mockito test fail

I have a problem with writing a test, to test my controller method. Here is my controller method:

@RequestMapping(value = "/customer/filter", method = RequestMethod.POST)
public String listCustomersSpecifiedByOrderCost(@RequestParam("filterValue") Double filterValue, Model model) {
    model.addAttribute("customers", customerService.listCustomersSpecifiedByOrders(filterValue));
    return "customersfiltered";
}

And there is my test:

@Test
public void testCustomersSpecifiedByOrderCost() throws Exception{
    List<Customer> customers = new ArrayList<>();
    customers.add(new Customer(1, "test1", "test1", "address1", 300.43));
    customers.add(new Customer(2, "test2", "test2", "address2", 340.43));
    customers.add(new Customer(3, "test3", "test3", "address3", 360.43));

    when(customerService.listCustomersSpecifiedByOrders(320.00)).thenReturn((List) customers);

    mockMvc.perform(post("/customer/filter"))
            .andExpect(status().isOk())
            .andExpect(view().name("customersfiltered"))
            .andExpect(model().attribute("customers", hasSize(2)));

}

The error is: org.springframework.web.bind.MissingServletRequestParameterException: Required Double parameter 'filterValue' is not present

The error status is 400

I don't know Mockito very well, how to test it properly?

Aucun commentaire:

Enregistrer un commentaire