如何使用 Mockito 部分模拟 HttpServletRequest

时间:2023-05-02
本文介绍了如何使用 Mockito 部分模拟 HttpServletRequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在模拟一个 HttpServletRequest ,在 servlet 调用中,请求中设置了新值,因为使用相同的请求我们将请求发送到某个 jsp,因此请求对象用作 servlet 的输入对象以及下一页的输出.

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.

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

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

我得到 null 因为我没有对 Request.getAttribute("a") 给出任何行为,我不能,这是我对下一页的响应,所以解释我需要 2 行为我的请求对象因此部分模拟,到目前为止,我无法窥探或对其进行任何部分嘲笑.有什么想法吗?

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?

代码:

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

推荐答案

你需要将属性存储到一个集合中:

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

这篇关于如何使用 Mockito 部分模拟 HttpServletRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:将泛型与 Mockito 匹配 下一篇:使用 Mockito 将模拟对象注入在测试中声明为字段的待测试对象不起作用?

相关文章