2017-10-07 140 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"; 
} 

をテストしようとすると、以下の私のコントローラから1を削除する方法を取得し、私のテストは

@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

更新チェックは、それはあなたの 'bookController'が' ReflectionTestUtils.setField'を使用する前に初期化されていないことになりましたエラーチェック@henri – valik

答えて

0

これは私にはうまく見えます。私がexpect(bookService.removeOne(anyObject(class));).andReturn(book);のタイプミスであると予想しているものからAppart。

何が間違っているか教えてください。例外とスタックトレースとは何ですか?また、bookControllernew BookController()を呼び出して作成されていることに同意しますか?ここ

+0

は何かということです。だからそれはnullです – valik

+0

で更新 – Henri

関連する問題