0

I'm trying to test my servlet to see if it calls my DAOService with some passed parameters from the session but running into this problem

The log:

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)

The code

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"));

Any tip is appreciated

3
  • 2
    MockHttpServletRequest isn't a Mockito recognised mock. Commented Dec 20, 2021 at 9:01
  • I was using that mock from Spring test as someone suggested in my previous question. I'm trying to do things in Mockito's way right now to see if there's any improvement, but the initiation so far is a pain Commented Dec 20, 2021 at 9:07
  • just use addParameter()javadoc (exclusive!)OR use Mockio... Commented Dec 20, 2021 at 9:18

2 Answers 2

1

As for initialising MockHttpServletRequest, you should use the Spring provided builder. Since it is a class offered by the Spring framework (and is not a Mockito mock), using Mockito to mock its methods will result in an error.

MockHttpServletResponse response = new MockHttpServletResponse();
MockHttpSession session = new MockHttpSession();
MockHttpServletRequest request = MockMvcRequestBuilders.get("/SupplierServlet")
    .session(session)
    .param("Name", "test")
    .param("Address", "test")
    .param("Phone", "1234")
    .buildRequest(new MockServletContext());

supplierServlet.processRequest(request, response);
supplierDAO = mock(SupplierDAO.class);
verify(supplierDAO).newSupplier(new Supplier("test", "test", "1234"));

Moreover, your supplierDAO mock is useless. After mocking an object, you need to inject it into the code under test. It is usually done by passing the mock as a function parameter. Whereas in your test, you're trying to verify calls on a mock that was never used.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, the supplierDAO part is to verify whether if it's called since it should be called if the servlet works as intended in the test
@LongDoan Yes, but the SupplierDAO (mock) instance in your test code is independent of the SupplierDAO instance in your servlet. You cannot verify calls on a (mocked) instance unless your servlet also uses the same instance.
0

Use mocks mocked by Mockito if you need to mock the behavior.

I was facing with the same error with following test method;

@Test
void testLogicWhenHttpCallShouldForwardToHttp() {
    //Given
    HttpServletRequest request = new MockHttpServletRequest();
    HttpServletResponse response = new MockHttpServletResponse();
    String url = "www.testdomain.com/api/route";

    //When
    Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("https://" + url));

    //... rest of the code
}

Fixed it by changing the mocking as said above;

@Test
void testLogicWhenHttpCallShouldForwardToHttp() {
    //Given
    HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
    HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
    String url = "www.testdomain.com/api/route";

    //When
    Mockito.when(request.getRequestURL()).thenReturn(new StringBuffer("https://" + url));

    //... rest of the code
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.