2017-10-07 12 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

여기에 무엇이 잘못 되었습니까? – valik

+0

업데이트 확인 – valik

답변

0

이것은 나에게 잘 들립니다. expect(bookService.removeOne(anyObject(class));).andReturn(book);에 오타가 될 것으로 예상되는 것으로부터 선택하십시오.

무엇이 잘못 되었습니까? 예외 및 스택 추적은 무엇입니까? 또한 을 호출하여 bookController을 만들었습니다.

+0

오류 확인 @henri – valik

+0

으로 업데이트되었습니다. 'bookController'가'ReflectionTestUtils.setField'를 사용하기 전에 초기화되지 않았 음을 나타냅니다. 그래서 그것은 null이다. – Henri