How to partially mock HttpServletRequest using Moc

2019-02-09 18:09发布

问题:

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

回答1:

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


回答2:

Spring's MockHttpServletRequest and MockHttpServletResponse are useful for that purpose. E.g.

    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    request.addHeader(HttpHeaders.HOST, "myhost.com");
    request.setLocalPort(PORT_VALID); // e.g. 8081
    request.setRemoteAddr(REQUEST_IP); // e.g. 127.0.0.1

then I can call myclass.method(request, response, ...) and check whether some attribute has been correctly set into the request, e.g.

    MyBean attr = (MyBean) request.getAttribute(ATTRIBUTE_NAME));
    // do my Assert.* stuff with 'attr'

MockHttpServletRequest and MockHttpServletResponse work fine where mock(HttpServletRequest.class) fails, for instance where you need to get back the real content of a request attribute which has been previously set within your business logic.



回答3:

Mockito supports real partial mock : http://docs.mockito.googlecode.com/hg/org/mockito/Mockito.html#16

I think it fits your needs