2015-12-28 30 views
1

指定@RequestMapping PARAMS有一個控制器:我如何MockMvc

@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"; 
    } 

這種控制器添加書DB的方法。方法有@RequestMapping註釋與params =「add」值。

進出口試圖設置此PARAMS標準控制器單元測試方法:

@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")); 
} 

在哪裏來指定此@ResuetsMapping PARAMS值?

此:

mockMvc.perform(後( 「/ bookForm」 「添加」))

不起作用的。

回答

0

以下應該工作。

mockMvc.perform(post("/bookForm?add="))

相關問題