如何使用 Mockito 部分模拟 HttpServletRequest

新手上路,请多包涵

我正在模拟一个 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);
}

原文由 Vivek 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 2.6k
2 个回答

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

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

原文由 Aure77 发布,翻译遵循 CC BY-SA 3.0 许可协议

Spring 的 MockHttpServletRequestMockHttpServletResponse 可用于此目的。例如

    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) 失败时工作正常,例如,您需要取回先前在业务逻辑中设置的请求属性的真实内容。

原文由 m c 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题