2017-10-08 27 views
0

它說錯誤在這種方法中,空指針異常顯示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"; 

    } 
+0

我們應該猜測這個NullPointerException被拋出的位置嗎?請發佈異常的堆棧跟蹤。對於我來說,第一行可能會拋出一個NullPointerException異常,因爲您試圖在調用之後模擬的bookService上調用findAll()方法。 – Kamil

+2

可能的重複[什麼是NullPointerException,以及如何解決它?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it ) – Kamil

+0

我添加了錯誤@Kamil – valik

回答

0

你必須使用它之前嘲笑bookService的。不在使用之後。那麼在@BeforeMethod中嘲弄bookService

+0

ok會嘗試 – valik

+0

正如例外所述,您需要在之前添加行爲。意思是,在使用bookService.findall() – pavan

+0

之前應該定義when()語句,但是我返回期望的Boo​​k列表的值是在book服務中找到的.find所有這些都是通過給出一個錯誤,因爲我在初始化之前使用了它@pavan – valik

相關問題