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

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

<tfoot id='Mhh36'></tfoot>
<legend id='Mhh36'><style id='Mhh36'><dir id='Mhh36'><q id='Mhh36'></q></dir></style></legend>

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

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

        Mockito,@InjectMocks 最終字段的奇怪行為

        Mockito, @InjectMocks strange behaviour with final fields(Mockito,@InjectMocks 最終字段的奇怪行為)

        • <tfoot id='8dSYQ'></tfoot>
            <bdo id='8dSYQ'></bdo><ul id='8dSYQ'></ul>

            <small id='8dSYQ'></small><noframes id='8dSYQ'>

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

                  <legend id='8dSYQ'><style id='8dSYQ'><dir id='8dSYQ'><q id='8dSYQ'></q></dir></style></legend>
                    <tbody id='8dSYQ'></tbody>
                1. 本文介紹了Mockito,@InjectMocks 最終字段的奇怪行為的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我看到我認為是錯誤的行為.@InjectMocks 似乎并沒有在每個測試方法之前創建一個新的測試主題.@Mock 在哪里.在以下示例中,如果 Subject.section 是最后一個 @Test 失敗.如果它不是最終的,則兩者都通過.我目前的解決方法是使用@BeforeClass,但這并不理想.

                  I am seeing behaviour that I believe is a bug. @InjectMocks does not seem to create a new test subject before every test method. Where as @Mock does. In the following example, if Subject.section is final one @Test fails. If its not final both pass. My current workaround is to use @BeforeClass, but this is not ideal.

                  主題.java:

                  package inject_mocks_test;
                  public class Subject {
                  
                      private final Section section;
                  
                      public Subject(Section section) {
                          this.section = section;
                      }
                  
                      public Section getSection() {
                          return section;
                      }
                  }
                  

                  Section.java:

                  Section.java:

                  package inject_mocks_test;
                  
                  public class Section {}
                  

                  SubjectTest.java

                  SubjectTest.java

                  package inject_mocks_test;
                  
                  import org.mockito.InjectMocks;
                  import org.mockito.Mock;
                  import org.mockito.MockitoAnnotations;
                  import org.testng.annotations.BeforeMethod;
                  import org.testng.annotations.Test;
                  
                  import static org.testng.Assert.assertEquals;
                  
                  public class SubjectTest {
                  
                      @Mock
                      Section section;
                  
                      @InjectMocks
                      Subject subject;
                  
                      @BeforeMethod
                      public void setup() {
                          MockitoAnnotations.initMocks(this);
                      }
                  
                      @Test
                      public void test1() {
                          assertEquals(section, subject.getSection());
                      }
                  
                      @Test
                      public void test2() {
                          assertEquals(section, subject.getSection());        
                      }
                  }
                  

                  干杯.

                  推薦答案

                  您正在使用 @InjectMocks 進行 constructor 注入.只要 Mockito 找到未初始化的字段(空),這將起作用.JUnit 在每次測試之前都會創建一個新的測試類實例,所以 JUnit 粉絲(像我一樣)永遠不會遇到這樣的問題.TestNg 沒有創建測試類的新實例.它保持測試方法之間的狀態,所以當 MockitoAnnotations.initMocks(this) 第二次調用時,Mockito 會發現 subject 字段已經初始化并嘗試使用 <強>場注入.這在另一回合將一直有效,直到該領域不是最終的.

                  You are using the @InjectMocks for constructor incjection. This will work as long as Mockito finds the field not initalized (null). JUnit is creating a new instance of the test class before each test, so JUnit fans (like me) will never face such problem. TestNg is not creating a new instance of test class. It's keeping the state between test methods, so when MockitoAnnotations.initMocks(this) is called for the second time, Mockito will find subject field already initialized and will try to use field injection. This on the other turn will work until the field is not final.

                  這是一個錯誤嗎?我相信不是——而是 API 設計的自然結果.一些解決方法是添加

                  Is this is a bug? I believe not - rather a natural consequence of the API design. Some workaround for you would be to add

                  this.subject = null;
                  

                  在一些 @AfterMethod 方法中.

                  這篇關于Mockito,@InjectMocks 最終字段的奇怪行為的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

                  相關文檔推薦

                  How can I detect integer overflow on 32 bits int?(如何檢測 32 位 int 上的整數溢出?)
                  Local variables before return statements, does it matter?(return 語句之前的局部變量,這有關系嗎?)
                  How to convert Integer to int?(如何將整數轉換為整數?)
                  How do I create an int array with randomly shuffled numbers in a given range(如何在給定范圍內創建一個隨機打亂數字的 int 數組)
                  Inconsistent behavior on java#39;s ==(java的行為不一致==)
                  Why is Java able to store 0xff000000 as an int?(為什么 Java 能夠將 0xff000000 存儲為 int?)
                2. <i id='s4Kjm'><tr id='s4Kjm'><dt id='s4Kjm'><q id='s4Kjm'><span id='s4Kjm'><b id='s4Kjm'><form id='s4Kjm'><ins id='s4Kjm'></ins><ul id='s4Kjm'></ul><sub id='s4Kjm'></sub></form><legend id='s4Kjm'></legend><bdo id='s4Kjm'><pre id='s4Kjm'><center id='s4Kjm'></center></pre></bdo></b><th id='s4Kjm'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='s4Kjm'><tfoot id='s4Kjm'></tfoot><dl id='s4Kjm'><fieldset id='s4Kjm'></fieldset></dl></div>

                    <tbody id='s4Kjm'></tbody>

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

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

                        • <tfoot id='s4Kjm'></tfoot><legend id='s4Kjm'><style id='s4Kjm'><dir id='s4Kjm'><q id='s4Kjm'></q></dir></style></legend>

                            主站蜘蛛池模板: 成人综合视频在线 | 亚洲精品不卡 | 蜜臀久久99精品久久久久野外 | av在线三级| 欧美激情视频一区二区三区在线播放 | 日本高清在线一区 | 男女视频网站 | 精品欧美一区二区三区久久久 | 国产清纯白嫩初高生在线播放视频 | 久久久久久免费毛片精品 | 黄久久久 | 欧洲免费视频 | 日韩欧美亚洲 | 特级特黄特色的免费大片 | 亚洲欧洲在线看 | www.日韩 | 激情欧美一区二区三区中文字幕 | 老外几下就让我高潮了 | 亚洲欧美日韩一区二区 | 国产激情视频在线 | 最新免费av网站 | 亚洲成网 | 中国一级特黄毛片大片 | 精品国产乱码久久久久久闺蜜 | 日本一区二区视频 | 日本视频一区二区三区 | 国产亚洲欧美日韩精品一区二区三区 | 日韩av电影在线观看 | 国产91在线播放 | 日韩网站在线 | 国内精品久久久久 | 91伦理片| 狠狠操狠狠操 | 天天看天天操 | www.99热.com | 国产成人午夜精品影院游乐网 | 一级高清 | 亚洲男女视频在线观看 | 成人深夜福利网站 | 中文字幕欧美日韩 | 日韩中文字幕一区二区 |