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

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

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

      1. Mockito thenReturn 返回相同的实例

        时间:2023-10-01

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

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

      2. <legend id='AGbfL'><style id='AGbfL'><dir id='AGbfL'><q id='AGbfL'></q></dir></style></legend>
        • <bdo id='AGbfL'></bdo><ul id='AGbfL'></ul>

                <tbody id='AGbfL'></tbody>

              <tfoot id='AGbfL'></tfoot>
                • 本文介绍了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 返回相同的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

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

                  相关文章

                  最新文章

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

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

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