lundi 28 décembre 2015

How to specify @RequestMapping params in MockMvc

I have a controller:

@Controller
@RequestMapping(value = "/bookForm")
public class BookFormController {

    @Autowired
    private BookHttpRequestParser parser;

    @Autowired
    private BooksService booksService;

    @RequestMapping(params = "add", method = RequestMethod.POST)
    public String addBook(HttpServletRequest request) {
        try {
            Book newBook = parser.createBookFromRequest(request);
            booksService.addBook(newBook);
        } catch (InvalidTypedParametersException e) {

        }
        return "redirect:index.html";
    }

This Controller has a method for adding book to DB. Method has @RequestMapping annotation with params ="add" value.

Im tring to set this params criteria to controller unit test method:

@Test
public void addBook() throws Exception{
    HttpServletRequest request = mock(HttpServletRequest.class);
    Book book = new Book();
    when(parser.createBookFromRequest(request)).thenReturn(book);

    mockMvc.perform(post("/bookForm", "add"))
    .andExpect(status().isOk())
    .andExpect(view().name("redirect:index.html"));
}

Where to specify this @ResuetsMapping params value?

This:

mockMvc.perform(post("/bookForm", "add"))

doesn't work at all.

Aucun commentaire:

Enregistrer un commentaire