Java Org.mockito.exceptions.misusing.MissingMethodInvocationException:怎么了?
我在这个测试中遇到了这个错误,我的代码有什么问题?我能做些什么来理解这个问题Java Org.mockito.exceptions.misusing.MissingMethodInvocationException:怎么了?,java,spring,Java,Spring,我在这个测试中遇到了这个错误,我的代码有什么问题?我能做些什么来理解这个问题 @InjectMocks BookController controller; @Mock BookService bookService; // will be inject to BookController MockMvc mockMvc; @Mock View mockView; @Bef
@InjectMocks
BookController controller;
@Mock
BookService bookService; // will be inject to BookController
MockMvc mockMvc;
@Mock
View mockView;
@Before
public void setUp() throws Exception {
new BookController(bookService);
MockitoAnnotations.initMocks(this);
mockMvc = standaloneSetup(controller)
.setSingleView(mockView)
.build();
}
@Test
public void showBookListPage() throws Exception {
securityService.autologin("admin","admin");
List<Book> expectedBookList = new ArrayList <Book>();
BookService bookService = mock(BookService.class);
when(bookService.findAll()).thenReturn(expectedBookList);
mockMvc.perform(get(" /book/bookList"))
.andExpect(view().name("bookList"))
.andExpect(model().attributeExists("bookList"))
.andExpect(model().attribute("bookList", hasItems(expectedBookList.toArray())));
}
一切似乎都是对的,但我总是得到这个错误这是因为你调用了
mock(BookService)
,我猜你也有注释;注入控制器的实例将不是您在()时调用的when
on。这是因为您调用了mock(BookService)
,并且我猜您还有注释;注入控制器的实例将不是您在()时调用的实例。
org.mockito.exceptions.misusing.MissingMethodInvocationException:
when() requires an argument which has to be 'a method call on a mock'.
For example:
when(mock.getArticles()).thenReturn(articles);
Also, this error might show up because:
1. you stub either of: final/private/equals()/hashCode() methods.
Those methods *cannot* be stubbed/verified.