如何处理服务类的mock创建并执行测试?

时间:2017-10-07 23:54:30

标签: java unit-testing mocking easymock

当尝试测试

时,从下面的控制器中删除一个方法会变得棘手
@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)

2 个答案:

答案 0 :(得分:0)

这对我来说很好看。公寓从我期望的expect(bookService.removeOne(anyObject( class));).andReturn(book);中的拼写错误。

你可以告诉我们出了什么问题吗?什么是异常和堆栈跟踪?另外,我们是否同意通过调用bookController创建new BookController()

答案 1 :(得分:0)

我知道很久以前有人问过它,但也许它会帮助某人:

当我忘记将 @RunWith(MockitoJUnitRunner.class) 放在我的测试类之上时,我遇到了那个错误。 如果您不指定它,则不会创建模拟并且您正在尝试在未初始化的对象上调用 ReflectionTestUtils。