Search code examples
javaunit-testingmockitojunit4

How to partially mock HttpServletRequest using Mockito


i am mocking a HttpServletRequest , in servlet call there are new values getting set in request because using same request we are dispaching request to some jsp so request object is used as a input object to servlet as well as output for next page.

i mocked all input parameters , but for all request.setAttribute() , my code is doing nothing as it's a mocked class , say if i have

request.setAttribute(a,"10")
System.out.println("a = " + request.getAttribute("a"));

i get null cuz i haven't given any behavious for Request.getAttribute("a") , and i can't , it's my response for next page , so that explain i need 2 behaviour my request object thus partial mocking , and i am unable to spy or do any partial mocking on it so far. any ideas?

Code :

 //Testcase
   Myservlet.java
public void doPost(request,response)
    {
         String a = request.getAttribute("a");
         String b = request.getAttribute("b");
         int sum = Integer.parseInt(a) + Integer.parseInt(b);
         request.setAttribute("sum",sum);
         //well in this example i can use sum what i calculated but in real senario i can't , i have to use request.getAttribute("sum")
         insertSumIntoDB(request.getAttribute("sum"));
    }
    }



  //testMyservlet.java
   @test
public void testServlet()
 {
 HttpServletRequest request = mock(HttpServletRequest.class);
     HttpServletResponse response = mock(HttpServletResponse.class);
when(request.getAttribute(a)).thenReturn("10");
when(request.getAttribute(b)).thenReturn("20");
new Myservlet(request,response);
}

Solution

  • You need to store attributes into a collection :

    // Collection to store attributes keys/values
    final Map<String, Object> attributes = new ConcurrentHashMap<String, Object>();     
    
    // Mock setAttribute
    Mockito.doAnswer(new Answer<Void>() {
        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            String key = invocation.getArgumentAt(0, String.class);
            Object value = invocation.getArgumentAt(1, Object.class);
            attributes.put(key, value);
            System.out.println("put attribute key="+key+", value="+value);
            return null;
        }
    }).when(request).setAttribute(Mockito.anyString(), Mockito.anyObject());
    
    // Mock getAttribute
    Mockito.doAnswer(new Answer<Object>() {
        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            String key = invocation.getArgumentAt(0, String.class);
            Object value = attributes.get(key);
            System.out.println("get attribute value for key="+key+" : "+value);
            return value;
        }
    }).when(request).getAttribute(Mockito.anyString());