<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 返回相同的实例

        Mockito thenReturn returns same instance(Mockito thenReturn 返回相同的实例)

        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 返回相同的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
                  How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
                  Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
                  Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
                  How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
                  How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)

                    <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>
                      <tbody id='h33pq'></tbody>
                    • <small id='h33pq'></small><noframes id='h33pq'>

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