久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

    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 class="qwawimqqmiuu" 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>

      2. Mockito @InjectMocks 不適用于相同類型的字段

        Mockito @InjectMocks doesn#39;t work for fields with same type(Mockito @InjectMocks 不適用于相同類型的字段)
                <tbody id='Fysdu'></tbody>
                <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 class="qwawimqqmiuu" id='Fysdu'><tfoot id='Fysdu'></tfoot><dl id='Fysdu'><fieldset id='Fysdu'></fieldset></dl></div>
              • <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 不適用于相同類型的字段的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

                  問(wèn)題描述

                  我很驚訝地發(fā)現(xiàn)以下簡(jiǎn)單的代碼示例不適用于所有 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 可能會(huì)感到困惑并且不會(huì)發(fā)生注入.

                  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.

                  這是否意味著如果我有多個(gè)具有相同類型的字段,我不能只模擬其中一個(gè),而是應(yīng)該為具有相同類型的 ALL 字段定義 @Mock?它是已知的限制嗎?是否有任何原因尚未修復(fù)?通過(guò)字段名稱匹配 @Mock 應(yīng)該很簡(jiǎn)單,不是嗎?

                  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?

                  推薦答案

                  看來(lái) Mockito 使用了 他們的JavaDoc

                  It appears Mockito uses an algorithm described in their JavaDoc

                  如果我理解正確,它將首先按類型排序(在這種情況下只有 1 B),然后按名稱排序(這里沒(méi)有更改).它最終將使用 OngoingInjector 接口實(shí)現(xiàn)注入,這似乎是搜索第一個(gè)字段并注入它.

                  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 個(gè) B 并且 Mock 中有 B 的 2 個(gè)字段,因此它將看到第一個(gè)實(shí)例與該字段的匹配并停止.這是因?yàn)?mocks.size() == 1注入/過(guò)濾器/NameBasedCandidateFilter.java">NameBasedCandidateFilter.因此它將停止過(guò)濾并直接注入.如果您創(chuàng)建多個(gè)相同類型的模擬,它們將按名稱排序并相應(yīng)地注入.

                  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.

                  當(dāng)我創(chuàng)建多個(gè)特定類型的模擬(但少于字段數(shù)量)時(shí),我能夠讓它工作.

                  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 個(gè)字段).

                  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).

                  您始終可以在他們的存儲(chǔ)庫(kù)中留下一個(gè) GitHub 問(wèn)題,并對(duì)注入過(guò)濾算法進(jìn)行增強(qiáng)或更改,以便查看.

                  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.

                  這篇關(guān)于Mockito @InjectMocks 不適用于相同類型的字段的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

                  相關(guān)文檔推薦

                  How can I detect integer overflow on 32 bits int?(如何檢測(cè) 32 位 int 上的整數(shù)溢出?)
                  Local variables before return statements, does it matter?(return 語(yǔ)句之前的局部變量,這有關(guān)系嗎?)
                  How to convert Integer to int?(如何將整數(shù)轉(zhuǎn)換為整數(shù)?)
                  How do I create an int array with randomly shuffled numbers in a given range(如何在給定范圍內(nèi)創(chuàng)建一個(gè)隨機(jī)打亂數(shù)字的 int 數(shù)組)
                  Inconsistent behavior on java#39;s ==(java的行為不一致==)
                  Why is Java able to store 0xff000000 as an int?(為什么 Java 能夠?qū)?0xff000000 存儲(chǔ)為 int?)
                        <tbody id='wL9f7'></tbody>
                    • <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 class="qwawimqqmiuu" 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>
                          • 主站蜘蛛池模板: 亚洲一区 | 久久三区| 国产成人久久精品 | 一区二区免费 | 午夜久久久久久久久久一区二区 | 国产伦精品一区二区三区高清 | 国产精品一区二区三区在线 | 亚洲福利视频网 | 成人免费毛片片v | 国产伦精品一区二区三区在线 | 成人依人| 999免费观看视频 | 91精品国产综合久久久久久首页 | 丁香五月缴情综合网 | 亚洲国产精品久久久久秋霞不卡 | 不卡的av在线 | 亚洲美女一区二区三区 | 国产精品不卡一区 | 美女天天操 | 在线观看成人免费视频 | www.久草.com| 九九亚洲精品 | 男人的天堂在线视频 | 亚洲国产成人精品久久 | 亚洲精品免费在线观看 | 日韩综合在线视频 | 亚洲综合第一页 | 欧美一级做a爰片免费视频 国产美女特级嫩嫩嫩bbb片 | 中文字幕第二区 | 精品在线一区二区三区 | 丁香一区二区 | 一级片在线播放 | 91久久久久久久久 | 视频一区二区中文字幕日韩 | 一区二区久久精品 | 日韩一区二区在线观看 | 日韩精品一区二区三区在线观看 | 91精品国产自产精品男人的天堂 | 欧美日韩国产在线 | 91影院 | 伊人激情网 |