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

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

        Mockito:doAnswer Vs thenReturn

        时间:2023-09-26
          <i id='vAr2u'><tr id='vAr2u'><dt id='vAr2u'><q id='vAr2u'><span id='vAr2u'><b id='vAr2u'><form id='vAr2u'><ins id='vAr2u'></ins><ul id='vAr2u'></ul><sub id='vAr2u'></sub></form><legend id='vAr2u'></legend><bdo id='vAr2u'><pre id='vAr2u'><center id='vAr2u'></center></pre></bdo></b><th id='vAr2u'></th></span></q></dt></tr></i><div id='vAr2u'><tfoot id='vAr2u'></tfoot><dl id='vAr2u'><fieldset id='vAr2u'></fieldset></dl></div>
            <bdo id='vAr2u'></bdo><ul id='vAr2u'></ul>

          • <small id='vAr2u'></small><noframes id='vAr2u'>

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

                  <tfoot id='vAr2u'></tfoot>

                1. 本文介绍了Mockito:doAnswer Vs thenReturn的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在使用 Mockito 进行后期单元测试.我对何时使用 doAnswerthenReturn 感到困惑.

                  I am using Mockito for service later unit testing. I am confused when to use doAnswer vs thenReturn.

                  谁能帮我详细介绍一下?到目前为止,我已经用 thenReturn 进行了尝试.

                  Can anyone help me in detail? So far, I have tried it with thenReturn.

                  推荐答案

                  当你在 mock 一个方法时知道返回值时,你应该使用 thenReturndoReturn称呼.调用模拟方法时会返回此定义的值.

                  You should use thenReturn or doReturn when you know the return value at the time you mock a method call. This defined value is returned when you invoke the mocked method.

                  thenReturn(T value) 设置调用方法时要返回的返回值.

                  thenReturn(T value) Sets a return value to be returned when the method is called.

                  @Test
                  public void test_return() throws Exception {
                      Dummy dummy = mock(Dummy.class);
                      int returnValue = 5;
                  
                      // choose your preferred way
                      when(dummy.stringLength("dummy")).thenReturn(returnValue);
                      doReturn(returnValue).when(dummy).stringLength("dummy");
                  }
                  

                  Answer 用于在调用模拟方法时需要执行其他操作,例如当需要根据该方法调用的参数计算返回值时.

                  Answer is used when you need to do additional actions when a mocked method is invoked, e.g. when you need to compute the return value based on the parameters of this method call.

                  当您想使用通用 Answer 存根 void 方法时,请使用 doAnswer().

                  Use doAnswer() when you want to stub a void method with generic Answer.

                  Answer 指定了一个执行的动作和一个在你与 mock 交互时返回的返回值.

                  Answer specifies an action that is executed and a return value that is returned when you interact with the mock.

                  @Test
                  public void test_answer() throws Exception {
                      Dummy dummy = mock(Dummy.class);
                      Answer<Integer> answer = new Answer<Integer>() {
                          public Integer answer(InvocationOnMock invocation) throws Throwable {
                              String string = invocation.getArgumentAt(0, String.class);
                              return string.length() * 2;
                          }
                      };
                  
                      // choose your preferred way
                      when(dummy.stringLength("dummy")).thenAnswer(answer);
                      doAnswer(answer).when(dummy).stringLength("dummy");
                  }
                  

                  这篇关于Mockito:doAnswer Vs thenReturn的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Mockito 可以在方法调用时根据参数的值来验证参数 下一篇:Java 使用 Mockito 验证 void 方法调用 n 次

                  相关文章

                  最新文章

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

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

                  <legend id='eAMan'><style id='eAMan'><dir id='eAMan'><q id='eAMan'></q></dir></style></legend>
                        <bdo id='eAMan'></bdo><ul id='eAMan'></ul>

                    1. <tfoot id='eAMan'></tfoot>