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

          <bdo id='ebtQp'></bdo><ul id='ebtQp'></ul>
        <i id='ebtQp'><tr id='ebtQp'><dt id='ebtQp'><q id='ebtQp'><span id='ebtQp'><b id='ebtQp'><form id='ebtQp'><ins id='ebtQp'></ins><ul id='ebtQp'></ul><sub id='ebtQp'></sub></form><legend id='ebtQp'></legend><bdo id='ebtQp'><pre id='ebtQp'><center id='ebtQp'></center></pre></bdo></b><th id='ebtQp'></th></span></q></dt></tr></i><div id='ebtQp'><tfoot id='ebtQp'></tfoot><dl id='ebtQp'><fieldset id='ebtQp'></fieldset></dl></div>
      2. Mockito thenReturn 返回相同的实例

        时间:2023-05-29

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

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

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

                  <tbody id='WinT6'></tbody>

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

                1. 本文介绍了Mockito thenReturn 返回相同的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在 Mockito 中有这个:

                  I have this in Mockito:

                  when(mockedMergeContext.createNewEntityOfType(IService.class)).thenReturn(new ServiceMock());
                  

                  createNewEntityOfType 方法应该总是返回一个新的 ServiceMock 实例,但它会返回两次相同的引用.

                  The createNewEntityOfType method should always return a new ServiceMock instance but it returns twice the same reference.

                  为什么 thenReturn 方法没有返回新的 ServiceMock?

                  Why the thenReturn method doesn't return new ServiceMock?

                  推荐答案

                  thenReturn 方法将始终返回传递给它的内容.new Servicemock() 代码在调用 thenReturn 之前被执行.然后将创建的 ServiceMock 传递给 thenReturn.因此 thenReturn 具有 ServiceMock 的绝对实例,而不是创建机制.

                  The thenReturn method will always return what is passed to it. The code new Servicemock() is being executed prior to the call to thenReturn. The created ServiceMock is then being passed to thenReturn. Therefore thenReturn has a absolute instance of ServiceMock not a creation mechanism.

                  如果您需要提供新实例,请使用 thenAnswer

                  If you need to provide an new instance, use thenAnswer

                  when(mockedMergeContext.createNewEntityOfType(IService.class))
                    .thenAnswer(new Answer<IService>() {
                       public IService answer(InvocationOnMock invocation) {
                          return new ServiceMock();
                       }
                     });
                  

                  这篇关于Mockito thenReturn 返回相同的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:我应该使用哪些 Maven 工件来导入 PowerMock? 下一篇:PowerMock 抛出 NoSuchMethodError (setMockName)

                  相关文章

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

                  <tfoot id='h33pq'></tfoot>
                2. <small id='h33pq'></small><noframes id='h33pq'>

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