<i id='raOwV'><tr id='raOwV'><dt id='raOwV'><q id='raOwV'><span id='raOwV'><b id='raOwV'><form id='raOwV'><ins id='raOwV'></ins><ul id='raOwV'></ul><sub id='raOwV'></sub></form><legend id='raOwV'></legend><bdo id='raOwV'><pre id='raOwV'><center id='raOwV'></center></pre></bdo></b><th id='raOwV'></th></span></q></dt></tr></i><div id='raOwV'><tfoot id='raOwV'></tfoot><dl id='raOwV'><fieldset id='raOwV'></fieldset></dl></div>
  • <small id='raOwV'></small><noframes id='raOwV'>

      1. <tfoot id='raOwV'></tfoot>
          <bdo id='raOwV'></bdo><ul id='raOwV'></ul>
        <legend id='raOwV'><style id='raOwV'><dir id='raOwV'><q id='raOwV'></q></dir></style></legend>

      2. 如何使用 Mockito 部分模拟 HttpServletRequest

        时间:2023-09-25

          1. <legend id='DyMZ1'><style id='DyMZ1'><dir id='DyMZ1'><q id='DyMZ1'></q></dir></style></legend>

            <small id='DyMZ1'></small><noframes id='DyMZ1'>

                <tbody id='DyMZ1'></tbody>

                  <bdo id='DyMZ1'></bdo><ul id='DyMZ1'></ul>
                • <i id='DyMZ1'><tr id='DyMZ1'><dt id='DyMZ1'><q id='DyMZ1'><span id='DyMZ1'><b id='DyMZ1'><form id='DyMZ1'><ins id='DyMZ1'></ins><ul id='DyMZ1'></ul><sub id='DyMZ1'></sub></form><legend id='DyMZ1'></legend><bdo id='DyMZ1'><pre id='DyMZ1'><center id='DyMZ1'></center></pre></bdo></b><th id='DyMZ1'></th></span></q></dt></tr></i><div id='DyMZ1'><tfoot id='DyMZ1'></tfoot><dl id='DyMZ1'><fieldset id='DyMZ1'></fieldset></dl></div>
                  <tfoot id='DyMZ1'></tfoot>

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

                  问题描述

                  我正在模拟一个 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

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

                  相关文章

                  最新文章

                • <tfoot id='bvVdf'></tfoot>
                  <legend id='bvVdf'><style id='bvVdf'><dir id='bvVdf'><q id='bvVdf'></q></dir></style></legend>

                • <i id='bvVdf'><tr id='bvVdf'><dt id='bvVdf'><q id='bvVdf'><span id='bvVdf'><b id='bvVdf'><form id='bvVdf'><ins id='bvVdf'></ins><ul id='bvVdf'></ul><sub id='bvVdf'></sub></form><legend id='bvVdf'></legend><bdo id='bvVdf'><pre id='bvVdf'><center id='bvVdf'></center></pre></bdo></b><th id='bvVdf'></th></span></q></dt></tr></i><div id='bvVdf'><tfoot id='bvVdf'></tfoot><dl id='bvVdf'><fieldset id='bvVdf'></fieldset></dl></div>

                    <small id='bvVdf'></small><noframes id='bvVdf'>

                        <bdo id='bvVdf'></bdo><ul id='bvVdf'></ul>