Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Org.mockito.exceptions.misusing.MissingMethodInvocationException:怎么了?_Java_Spring - Fatal编程技术网

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.