2017-10-07 54 views
0

它當它到達方法從我的控制器中刪除一個得到棘手試圖測試我如何處理爲服務類創建模擬並執行測試?

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

時的下方,我的測試與模擬注射開始在我之前的測試方法如下

@Before 
public void setUp() { 
    bookService = createMock(BookService.class); 

    ReflectionTestUtils.setField(bookController, "bookService", bookService); 

    userRepository= createMock(UserRepository.class); 
    ReflectionTestUtils.setField(bookController, "userRepository", userRepository); 

    mockMvc = standaloneSetup(bookController) 
      .setMessageConverters(new MappingJackson2HttpMessageConverter()) 
      .build(); 
} 

最後的測試,試圖刪除一本書,而不是寫它。我該如何繼續?

@Test 
public void bookRemoveTest() throws Exception { 
    securityService.autologin("admin", "admin"); 

    Book book = new Book(); 
    book.setId(1L); 

    expect(bookService.removeOne(anyObject(class));).andReturn(book); 

    replay(bookService); 

    MvcResult result = mockMvc 
      .perform(post("/book/remove") 
        .accept(MediaType.TEXT_HTML) 
        .contentType(MediaType.TEXT_HTML)) 
      .andExpect(status().isOk()) 
      .andReturn(); 
    String controllerResult = result.getResponse().getContentAsString(); 
} 

哪裏出了問題,我該如何使這個測試工作?

java.lang.IllegalArgumentException: Either targetObject or targetClass for the field must be specified 

    at org.springframework.util.Assert.isTrue(Assert.java:68) 
    at org.springframework.test.util.ReflectionTestUtils.setField(ReflectionTestUtils.java:164) 
    at org.springframework.test.util.ReflectionTestUtils.setField(ReflectionTestUtils.java:100) 
    at org.springframework.test.util.ReflectionTestUtils.setField(ReflectionTestUtils.java:84) 
    at com.admintest.controller.BookControllerTest.setUp1(BookControllerTest.java:62) 
    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.RunBefores.evaluate(RunBefores.java:24) 
+0

這裏是什麼錯@Stephen – valik

+0

已更新現在檢查 – valik

回答

0

這對我來說很好。公寓從我預計會在expect(bookService.removeOne(anyObject(class));).andReturn(book);打字錯誤。

您能告訴我們什麼是錯的嗎?什麼是異常和堆棧跟蹤?另外,我們是否同意通過撥打new BookController()創建bookController

+0

更新錯誤檢查@henri – valik

+0

這意味着您的'bookController'在使用'ReflectionTestUtils.setField'之前未被初始化。所以它是空的 – Henri

相關問題