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

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

      1. Mockito @InjectMocks 不适用于相同类型的字段

        时间:2023-10-01

        1. <small id='o7Zww'></small><noframes id='o7Zww'>

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

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

                • 本文介绍了Mockito @InjectMocks 不适用于相同类型的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我很惊讶地发现以下简单的代码示例不适用于所有 Mockito 版本 > 1.8.5

                  I was very surprised to find out that following simple code example doesn't work for all Mockito versions > 1.8.5

                  @RunWith(MockitoJUnitRunner.class)
                  public class MockitoTest {
                  
                      @Mock(name = "b2")
                      private B b2;
                  
                      @InjectMocks
                      private A a;
                  
                      @Test
                      public void testInjection() throws Exception {
                          assertNotNull(a.b2); //fails
                          assertNull(a.b1); //also fails, because unexpectedly b2 mock gets injected here
                      }
                  
                      static class A{
                          private B b1;
                          private B b2;
                      }
                  
                      interface B{}
                  }
                  

                  在 javadocs (http://docs.mockito.googlecode.com/hg/latest/org/mockito/InjectMocks.html)有一句话:

                  In javadocs (http://docs.mockito.googlecode.com/hg/latest/org/mockito/InjectMocks.html) there is a quote:

                  注意 1:如果您有相同类型(或相同擦除)的字段,则为最好用匹配字段命名所有 @Mock 注释字段,否则 Mockito 可能会感到困惑并且不会发生注入.

                  Note 1: If you have fields with the same type (or same erasure), it's better to name all @Mock annotated fields with the matching fields, otherwise Mockito might get confused and injection won't happen.

                  这是否意味着如果我有多个具有相同类型的字段,我不能只模拟其中一个,而是应该为具有相同类型的 ALL 字段定义 @Mock?它是已知的限制吗?是否有任何原因尚未修复?通过字段名称匹配 @Mock 应该很简单,不是吗?

                  Does it mean that if I have several fields with same type I can't mock ONLY ONE of them but rather should define @Mock for ALL fields with same type? Is it known limitation and is there any reason why it wasn't fixed yet? It should be straightforward to match @Mock by fields names, isn't it?

                  推荐答案

                  看来 Mockito 使用了 他们的JavaDoc

                  It appears Mockito uses an algorithm described in their JavaDoc

                  如果我理解正确,它将首先按类型排序(在这种情况下只有 1 B),然后按名称排序(这里没有更改).它最终将使用 OngoingInjector 接口实现注入,这似乎是搜索第一个字段并注入它.

                  If I understand correctly, it will first sort on type (in this case only 1 B) and then sort on name (no changes here). It will finally inject using the OngoingInjector interface implementation, which appears to search for the first field and inject it.

                  由于您只定义了 1 个 B 并且 Mock 中有 B 的 2 个字段,因此它将看到第一个实例与该字段的匹配并停止.这是因为 mocks.size() == 1注入/过滤器/NameBasedCandidateFilter.java">NameBasedCandidateFilter.因此它将停止过滤并直接注入.如果您创建多个相同类型的模拟,它们将按名称排序并相应地注入.

                  Since you only have 1 B defined and there are 2 fields of B in the Mock, it will see the match of the first instance to the field and stop. This is because mocks.size() == 1 in NameBasedCandidateFilter . Therefore it will stop filtering and inject it directly. If you create multiple mocks of the same type, they will get sorted on Name and injected accordingly.

                  当我创建多个特定类型的模拟(但少于字段数量)时,我能够让它工作.

                  I was able to get it work when I created multiple mocks (but less than the number of fields) of a specific type.

                  @RunWith(MockitoJUnitRunner.class)
                  public class MockitoTest {
                  
                      @Mock(name = "b2")
                      private B b2;
                  
                      @Mock(name = "b3")
                      private B b3;
                  
                      @InjectMocks
                      private A a;
                  
                      @Test
                      public void testInjection() {
                          System.out.println(this.a);
                      }
                  
                      static class A {
                  
                          private B b1;
                  
                          private B b2;
                  
                          private B b3;
                      }
                  
                      interface B {
                      }
                  }
                  

                  这将正确地将 b2 注入 a.b2 并将 b3 注入 a.b3 而不是 a.b1 和 a.b2(在 A 中定义的前 2 个字段).

                  This will correctly inject b2 into a.b2 and b3 into a.b3 instead of a.b1 and a.b2 (the first 2 fields that are defined in A).

                  您始终可以在他们的存储库中留下一个 GitHub 问题,并对注入过滤算法进行增强或更改,以便查看.

                  You can always leave a GitHub issue on their repository with an enhancement or change on the injection filtering algorithm in order to be looked at.

                  这篇关于Mockito @InjectMocks 不适用于相同类型的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:java.util.MissingResourceException:找不到基本名称 java 下一篇:Android Jack mockito 替代品

                  相关文章

                  最新文章

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

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

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

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

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