【发布时间】:2022-01-21 21:59:13
【问题描述】:
我正在尝试测试我的 servlet,看看它是否使用会话中传递的一些参数调用我的 DAOService,但遇到了这个问题
日志:
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);
at Servlet.SupplierServletTest.supplierServlet_StandardTest(SupplierServletTest.java:32)
代码
SupplierServlet supplierServlet = new SupplierServlet();
MockHttpServletRequest request = new MockMvcRequestBuilders.get("/SupplierServlet").buildRequest(new MockServletContext());
MockHttpServletResponse response = new MockHttpServletResponse();
MockHttpSession session = new MockHttpSession();
when(request.getSession()).thenReturn(session); //This is the line 32 that the log mentioned, I deleted the session part and the problem was the same for the following lines
when(request.getParameter("Name")).thenReturn("test");
when(request.getParameter("Address")).thenReturn("test");
when(request.getParameter("Phone")).thenReturn("1234");
supplierServlet.processRequest(request, response);
supplierDAO = mock(SupplierDAO.class);
verify(supplierDAO).newSupplier(new Supplier("test", "test", "1234"));
感谢任何提示
【问题讨论】:
-
MockHttpServletRequest 不是 Mockito 识别的模拟。
-
我正在使用 Spring 测试中的模拟,正如我在上一个问题中所建议的那样。我现在正在尝试按照 Mockito 的方式做事,看看是否有任何改进,但到目前为止的启动很痛苦
-
只需使用
addParameter()javadoc(独家!)或使用Mockio...
标签: java unit-testing servlets mockito