<legend id='XDlCZ'><style id='XDlCZ'><dir id='XDlCZ'><q id='XDlCZ'></q></dir></style></legend>

        • <bdo id='XDlCZ'></bdo><ul id='XDlCZ'></ul>

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

        <tfoot id='XDlCZ'></tfoot>

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

        编写 JUnit 测试用例请求调度程序时出错

        时间:2023-09-25

            <tbody id='o0HAA'></tbody>
          <legend id='o0HAA'><style id='o0HAA'><dir id='o0HAA'><q id='o0HAA'></q></dir></style></legend>

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

              • <bdo id='o0HAA'></bdo><ul id='o0HAA'></ul>
                  <tfoot id='o0HAA'></tfoot>
                  本文介绍了编写 JUnit 测试用例请求调度程序时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我在为请求调度程序编写测试用例时遇到了一些错误.我的课

                  I am facing some error while writing test case for Request dispatcher. My class

                  @Override
                          public void doFilter(ServletRequest request, ServletResponse resp, FilterChain chain)
                              throws IOException, ServletException
                          {
                              if(isMockAccountEnabled())
                              {
                                  HttpServletRequest req = (HttpServletRequest)request;
                                  String reqUrl = req.getRequestURI();
                                  ApiUserDetails userDetails = userBean.getUserDetails();
                                  HttpSession session = req.getSession();
                                  if(isThisTestAccount(reqUrl, session))
                                  {
                                      log.info(userDetails);
                                      log.debug("Entering Test acount flow for the request "+reqUrl);
                                      RequestDispatcher dispatcher = req.getRequestDispatcher("/mock/" + EnumService.returnMockService(reqUrl));
                                      dispatcher.forward(request, resp);
                                  }
                              }
                          }
                  

                  编写测试用例

                  @Mock
                  private FilterChain chain;
                  
                  
                  @InjectMocks
                  private MockAccountFilter mockAccountFilter = new MockAccountFilter();
                  
                  
                  MockHttpServletRequest request = new MockHttpServletRequest();
                  MockHttpServletResponse response = new MockHttpServletResponse();
                  MockHttpSession session = new MockHttpSession();
                  @Test
                  public void filterRequestMockFirst()
                      throws Exception
                  {
                      MockRequestDispatcher dispatcher =new MockRequestDispatcher("/mock/ABCTEST");
                      when(request.getRequestDispatcher("/mock/ABCTEST")).thenReturn(dispatcher);
                      request.setRequestURI("/check/employee/123456/false");
                      mockAccountFilter.doFilter(request, response, chain);  
                      Assert.assertTrue(request.getRequestURI().contains("/mock/ABCTEST"));
                  
                  }
                  

                  错误

                  when() requires an argument which has to be 'a method call on a mock'.
                  

                  谁能告诉我编写这个测试用例的确切方法.

                  Can some one tell me the exact way of writing this test case.

                  推荐答案

                  我没有足够的信息告诉你编写这个测试用例的确切方法",而且 StackOverflow 不是一个获取大块的好地方代码已修复,但我可以告诉您为什么您会收到该消息.:)

                  I don't have enough information to tell you "the exact way of writing this test case", and StackOverflow isn't a good place to get large blocks of code fixed, but I can tell you why you're getting that message. :)

                  MockHttpServletRequest request = new MockHttpServletRequest();
                  

                  这里有两种模拟"的感觉:

                  There are two senses of "Mock" going on here:

                  1. Mockito 提供的模拟是基于接口自动生成的,并使用 whenverify 等静态方法进行操作.Mockito 模拟是使用 Mockito.mock(或 @Mock 当且仅当您使用 MockitoJUnitRunnerMockitoAnnotations.initMocks).

                  1. Mockito-provided mocks are automatically generated based on interfaces, and are manipulated with static methods like when and verify. Mockito mocks are created using Mockito.mock (or @Mock if and only if you use MockitoJUnitRunner or MockitoAnnotations.initMocks).

                  名称以Mock"开头的完整类,例如 MockHttpServletRequest,实际上是整个类的实现,碰巧比您通过 J2EE 实际接收的更容易变异或更改.这些可能更准确地称为假",因为它们是用于测试的简单接口实现,不验证行为并且不通过 Mockito 工作.您可以确定它们不是 Mockito 模拟,因为您使用 new MockHttpServletRequest(); 实例化它们.

                  Full classes with names starting with the word "Mock", like MockHttpServletRequest, are actually entire class implementations that happen to be easier to mutate or change than ones you would actually receive through J2EE. These might more accurately be termed "Fake", because they are simple interface implementations for testing that do not verify behavior and do not work through Mockito. You can tell for sure that they're not Mockito mocks because you instantiate them with new MockHttpServletRequest();.

                  例如,FilterChain 很可能由 Mockito 提供.MockHttpServletRequest request 不是 Mockito 模拟,这就是您收到错误消息的原因.

                  FilterChain, for instance, will likely be provided by Mockito. MockHttpServletRequest request is not a Mockito mock, which is why you're getting the error message you're getting.

                  您最好的选择是选择一种类型的模拟或另一种 - 任何一种都可以工作 - 并确保使用 when 语句(如果您选择 Mockito)正确准备这些模拟或像 setRequestURI 这样的设置器(如果您选择 MockHttpSession 样式的模拟).

                  Your best bet is to pick one type of mock or the other—either will work—and make sure that you're preparing those mocks properly with the when statement (if you choose Mockito) or setters like setRequestURI (if you choose the MockHttpSession-style mocks).

                  这篇关于编写 JUnit 测试用例请求调度程序时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:使用 Mockito 2.0.7 模拟 lambda 表达式 下一篇:当父级在不同的包中时模拟受保护的父级方法

                  相关文章

                  最新文章

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

                    1. <small id='5D2Gx'></small><noframes id='5D2Gx'>

                    2. <legend id='5D2Gx'><style id='5D2Gx'><dir id='5D2Gx'><q id='5D2Gx'></q></dir></style></legend>
                      <tfoot id='5D2Gx'></tfoot>
                        <bdo id='5D2Gx'></bdo><ul id='5D2Gx'></ul>