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

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

        <bdo id='vmBIt'></bdo><ul id='vmBIt'></ul>
      <tfoot id='vmBIt'></tfoot>

      1. doThrow() doAnswer() doNothing() 和 doReturn() 在 mockito 中

        时间:2023-09-26

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

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

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

            <tbody id='yI76J'></tbody>

                • 本文介绍了doThrow() doAnswer() doNothing() 和 doReturn() 在 mockito 中的用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我在学习mockito,我从链接.

                  I was learning mockito and I understood the basic usages of the above mentioned functions from the link.

                  但是我想知道它是否可以用于任何其他情况?

                  But I would like to know whether it can be used for any other cases?

                  推荐答案

                  doThrow : 主要用于在模拟对象中调用方法时抛出异常.

                  doThrow : Basically used when you want to throw an exception when a method is being called within a mock object.

                  public void validateEntity(final Object object){}
                  Mockito.doThrow(IllegalArgumentException.class)
                  .when(validationService).validateEntity(Matchers.any(AnyObjectClass.class));
                  

                  doReturn : 当你想在方法执行时返回一个返回值时使用.

                  doReturn : Used when you want to send back a return value when a method is executed.

                  public Socket getCosmosSocket() throws IOException {}
                  Mockito.doReturn(cosmosSocket).when(cosmosServiceImpl).getCosmosSocket();
                  

                  doAnswer:有时您需要对传递给方法的参数执行一些操作,例如,添加一些值、进行一些计算甚至修改它们 doAnswer 给您答案

                  doAnswer: Sometimes you need to do some actions with the arguments that are passed to the method, for example, add some values, make some calculations or even modify them doAnswer gives you the Answer<?> interface that being executed in the moment that method is called, this interface allows you to interact with the parameters via the InvocationOnMock argument. Also, the return value of answer method will be the return value of the mocked method.

                  public ReturnValueObject quickChange(Object1 object);
                  Mockito.doAnswer(new Answer<ReturnValueObject>() {
                  
                          @Override
                          public ReturnValueObject answer(final InvocationOnMock invocation) throws Throwable {
                  
                              final Object1 originalArgument = (invocation.getArguments())[0];
                              final ReturnValueObject returnedValue = new ReturnValueObject();
                              returnedValue.setCost(new Cost());
                  
                              return returnedValue ;
                          }
                  }).when(priceChangeRequestService).quickCharge(Matchers.any(Object1.class));
                  

                  doNothing:(来自 文档)使用 doNothing() 将 void 方法设置为不执行任何操作.请注意,模拟上的 void 方法默认情况下什么都不做!但是,doNothing() 派上用场的情况很少见:

                  doNothing: (From documentation) Use doNothing() for setting void methods to do nothing. Beware that void methods on mocks do nothing by default! However, there are rare situations when doNothing() comes handy:

                  • 对 void 方法的连续调用存根:

                  • Stubbing consecutive calls on a void method:

                  doNothing().
                  doThrow(new RuntimeException())
                  .when(mock).someVoidMethod();
                  
                  //does nothing the first time:
                  mock.someVoidMethod();
                  
                  //throws RuntimeException the next time:
                  mock.someVoidMethod();
                  

                • 当你窥探真实的对象并且你想让 void 方法什么都不做时:

                • When you spy real objects and you want the void method to do nothing:

                  List list = new LinkedList();
                  List spy = spy(list);
                  
                  //let's make clear() do nothing
                  doNothing().when(spy).clear();
                  
                  spy.add("one");
                  
                  //clear() does nothing, so the list still contains "one"
                  spy.clear();
                  

                • 这篇关于doThrow() doAnswer() doNothing() 和 doReturn() 在 mockito 中的用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:何时使用 Mockito.verify()? 下一篇:如何验证具有不同参数的多个方法调用

                  相关文章

                  最新文章

                  <tfoot id='cqMcF'></tfoot>

                      <legend id='cqMcF'><style id='cqMcF'><dir id='cqMcF'><q id='cqMcF'></q></dir></style></legend>
                    1. <small id='cqMcF'></small><noframes id='cqMcF'>

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