如何使用Mockito部分模拟HttpServletRequest

时间:2014-03-28 13:33:16

标签: java unit-testing mockito junit4

我在模拟一个HttpServletRequest,在servlet调用中有新的值在请求中设置,因为使用相同的请求我们正在向一些jsp调度请求,因此请求对象被用作servlet的输入对象以及下一页的输出。

我模拟了所有的输入参数,但是对于所有的request.setAttribute(),我的代码什么也没做,因为它是一个模拟的类,比如说我有

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

我得到null因为我没有给Request.getAttribute(“a”)任何行为,我不能,这是我对下一页的回应,所以解释我需要2个行为我的请求对象因此部分嘲笑,到目前为止,我无法间谍或做任何部分嘲弄。任何想法?

代码:

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

3 个答案:

答案 0 :(得分:11)

Spring的MockHttpServletRequestMockHttpServletResponse对此非常有用。 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

然后我可以调用 myclass.method(request,response,...)并检查某个属性是否已正确设置到请求中,例如

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

MockHttpServletRequest和MockHttpServletResponse在 mock(HttpServletRequest.class)失败时正常工作,例如,您需要获取先前已在业务逻辑中设置的请求属性的实际内容。< / p>

答案 1 :(得分:5)

您需要将属性存储到集合中:

// 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 :(得分:1)

Mockito支持真正的部分模拟:http://docs.mockito.googlecode.com/hg/org/mockito/Mockito.html#16

我认为它符合您的需求