我的测试在第97行有一个错误我应该知道如何模仿从我的控制器处理的正确价值
@RequestMapping(value = "/remove", method = RequestMethod.POST)
public String remove(
@ModelAttribute( "id" ) String id, Model model
) {
bookService.removeOne(Long.parseLong(id.substring(8)));
List<Book> bookList = bookService.findAll();
model.addAttribute("bookList", bookList);
return "redirect:/book/bookList";
}
该方法删除id为subs substring 8的方法,如何在我的测试中表示。 具体错误写在下面
@Mock
BookService bookService; // will be inject to BookController
MockMvc mockMvc;
@Before
public void setUp() throws Exception {
new BookController(bookService);
MockitoAnnotations.initMocks(this);
mockMvc = standaloneSetup(controller)
.setSingleView(new InternalResourceView("/book/bookList"))
.build();
}
@Test
public void bookRemoveTest() throws Exception {
securityService.autologin("admin", "admin");
Book book = new Book();
book.setId(1L);
BookService bookService = mock(BookService.class);
bookService.removeOne( book.getId());
expect(bookService.findOne(anyLong())).andReturn(book);
replay(bookService);
MvcResult result = mockMvc
.perform(post("/book/remove")
.accept(MediaType.TEXT_HTML)
.contentType(MediaType.TEXT_HTML))
.andExpect(status().isOk())
.andExpect(model().attributeExists("bookList"))
.andExpect(content().contentType(MediaType.TEXT_HTML))
.andReturn();
}
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.StringIndexOutOfBoundsException: String index out of range: -8
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:982)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at org.springframework.test.web.servlet.TestDispatcherServlet.service(TestDispatcherServlet.java:65)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.springframework.mock.web.MockFilterChain$ServletFilterProxy.doFilter(MockFilterChain.java:167)
at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:134)
at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:155)
at com.admintest.controller.BookControllerTest.bookRemoveTest(BookControllerTest.java:97)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.lang.reflect.Method.invoke(Method.java:498)
at .evaluate(RunBefores.java:26)