它說錯誤在這種方法中,空指針異常顯示java.lang.NullPointerException什麼是錯的
@Test
public void showBookListPage() throws Exception {
List<Book> expectedBookList = bookService.findAll();
BookService bookService = mock(BookService.class);
when(bookService.findAll()).thenReturn(expectedBookList);
BookController bookController = new BookController(bookService);
MockMvc mockMvc = standaloneSetup(bookController).build();
mockMvc.perform(get(" /book/bookList"))
.andExpect(view().name("bookList"))
.andExpect(model().attributeExists("bookList"))
.andExpect(model().attribute("bookList", hasItems(expectedBookList.toArray())));
}
}
比其他一切似乎是正確的
這是我嘲笑這本書的售後服務得到了錯誤第一次調用之前
java.lang.IllegalStateException: missing behavior definition for the preceding method call:
BookService.findAll()
Usage is: expect(a.foo()).andXXX()
at org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:42)
at org.easymock.internal.ObjectMethodsFilter.invoke(ObjectMethodsFilter.java:94)
at com.sun.proxy.$Proxy134.findAll(Unknown Source)
at com.admintest.controller.BookControllerTest.showBookListPage(BookControllerTest.java:101)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springf
這是我編輯的測試方法 @Test 公共無效showBookListPage T() hrows異常{
BookService bookService = mock(BookService.class);
List<Book> expectedBookList = bookService.findAll();
when(bookService.findAll()).thenReturn(expectedBookList);
BookController bookController = new BookController(bookService);
MockMvc mockMvc = standaloneSetup(bookController).build();
mockMvc.perform(get(" /book/bookList"))
.andExpect(view().name("bookList"))
.andExpect(model().attributeExists("bookList"))
.andExpect(model().attribute("bookList", hasItems(expectedBookList.toArray())));
}
順便說一句,這是控制器
@RequestMapping("/bookList")
public String bookList(Model model) {
List<Book> bookList = bookService.findAll();
model.addAttribute("bookList", bookList);
return "bookList";
}
我們應該猜測這個NullPointerException被拋出的位置嗎?請發佈異常的堆棧跟蹤。對於我來說,第一行可能會拋出一個NullPointerException異常,因爲您試圖在調用之後模擬的bookService上調用findAll()方法。 – Kamil
可能的重複[什麼是NullPointerException,以及如何解決它?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it ) – Kamil
我添加了錯誤@Kamil – valik