2017-10-08 1 views
0

Il dit erreur dans cette méthode un pointeur NULL ExceptionJava.lang.NullPointerException ce qui est erroné

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

Autre que que tout semble être correct

C'est l'erreur que je suis arrivé après se moquant du service de livres d'abord avant l'appel

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 

Et voici ma modifier la méthode de test @Test de public void showBookListPage() t hrows Exception {

 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()))); 
} 

Et par la façon dont c'est le contrôleur

@RequestMapping("/bookList") 
    public String bookList(Model model) { 
     List<Book> bookList = bookService.findAll(); 
     model.addAttribute("bookList", bookList); 
     return "bookList"; 

    } 
+0

Faut-il deviner où cette NullPointerException est levée? Veuillez également publier une trace de pile pour l'exception. Pour moi, la toute première ligne peut lancer une exception NullPointerException, car vous essayez d'appeler la méthode findAll() sur bookService qui est raillé après cet appel. – Kamil

+2

Copie possible de [Qu'est-ce qu'une exception NullPointerException et comment la réparer?] (Https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it – Kamil

+0

J'ai ajouté des erreurs @Kamil – valik

Répondre

0

Vous devez se moquer de la Bookservice avant de l'utiliser. Pas après son utilisation. Donc, le moqueur de bookService dans @BeforeMethod

+0

ok va essayer que – valik

+0

Comme l'indique l'exception, vous devez ajouter le comportement avant. Moyens, when() instruction doit être définie avant d'utiliser bookService.findall() – pavan

+0

mais la valeur que je renvoie attendu La liste de livres se trouve dans le service de livre .find Tous là en donnant une erreur car je l'utilise avant l'initialisation @pavan – valik