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

    <bdo id='BBXjD'></bdo><ul id='BBXjD'></ul>
      1. <legend id='BBXjD'><style id='BBXjD'><dir id='BBXjD'><q id='BBXjD'></q></dir></style></legend>

        <tfoot id='BBXjD'></tfoot>

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

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

        為什么我們不能使用 Mockito 為參數化構造函數創

        why cannot we create spy for Parameterized Constructor using Mockito(為什么我們不能使用 Mockito 為參數化構造函數創建間諜)

      2. <tfoot id='93cwy'></tfoot>
          <tbody id='93cwy'></tbody>

            <legend id='93cwy'><style id='93cwy'><dir id='93cwy'><q id='93cwy'></q></dir></style></legend>
          1. <i id='93cwy'><tr id='93cwy'><dt id='93cwy'><q id='93cwy'><span id='93cwy'><b id='93cwy'><form id='93cwy'><ins id='93cwy'></ins><ul id='93cwy'></ul><sub id='93cwy'></sub></form><legend id='93cwy'></legend><bdo id='93cwy'><pre id='93cwy'><center id='93cwy'></center></pre></bdo></b><th id='93cwy'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='93cwy'><tfoot id='93cwy'></tfoot><dl id='93cwy'><fieldset id='93cwy'></fieldset></dl></div>
            • <bdo id='93cwy'></bdo><ul id='93cwy'></ul>
            • <small id='93cwy'></small><noframes id='93cwy'>

                  本文介紹了為什么我們不能使用 Mockito 為參數化構造函數創建間諜的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我的代碼中只有參數化的構造函數,我需要通過它進行注入.

                  I have only parameterized constructor in my code and i need to inject through it.

                  我想監視參數化構造函數以注入模擬對象作為我的 junit 的依賴項.

                  I want to spy parameterized constructor to inject mock object as dependency for my junit.

                  public RegDao(){
                   //original object instantiation here
                  Notification ....
                  EntryService .....
                  }
                  
                  public RegDao(Notification notification , EntryService entry) {
                   // initialize here
                  }
                  
                  we have something like below : 
                  RegDao dao = Mockito.spy(RegDao.class);
                  

                  但是我們有什么東西可以讓我在構造函數中注入模擬對象并監視它嗎?

                  But do we have something that i can inject mocked object in the Constructor and spy it?.

                  推薦答案

                  您可以通過在 junit 中使用參數化構造函數實例化您的主類,然后從中創建一個間諜來做到這一點.

                  You can do that by instantiating your main class with parametrized constructor in your junit and then creating a spy from it.

                  假設您的主類是A.其中 BC 是它的依賴項

                  Let's suppose your main class is A. Where B and C are its dependencies

                  public class A {
                  
                      private B b;
                  
                      private C c;
                  
                      public A(B b,C c)
                      {
                          this.b=b;
                          this.c=c;
                      }
                  
                      void method() {
                          System.out.println("A's method called");
                          b.method();
                          c.method();
                          System.out.println(method2());
                  
                      }
                  
                      protected int method2() {
                          return 10;
                      }
                  }
                  

                  然后您可以使用下面的參數化類為此編寫 junit

                  Then you can write junit for this using your parametrized class as below

                  @RunWith(MockitoJUnitRunner.class)
                  public class ATest {
                  
                      A a;
                  
                      @Mock
                      B b;
                  
                      @Mock
                      C c;
                  
                      @Test
                      public void test() {
                          a=new A(b, c);
                          A spyA=Mockito.spy(a);
                  
                          doReturn(20).when(spyA).method2();
                  
                          spyA.method();
                      }
                  }
                  

                  測試類的輸出

                  A's method called
                  20
                  

                  1. 這里 BC 是您使用參數化構造函數注入到您的類 A 中的模擬對象.
                  2. 然后我們創建了一個名為 spyAAspy.
                  3. 我們通過修改類 A 中受保護方法 method2 的返回值來檢查 spy 是否真的有效,這不可能如果 spyA 不是 A 的實際 spy.
                  1. Here B and C are mocked object that you injected in your class A using parametrized constructor.
                  2. Then we created a spy of A called spyA.
                  3. We checked if spy is really working by modifying the return value of a protected method method2 in class A which could not have been possible if spyA was not an actual spy of A.

                  這篇關于為什么我們不能使用 Mockito 為參數化構造函數創建間諜的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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?)

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

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

                      <tfoot id='ChcLK'></tfoot>
                        <bdo id='ChcLK'></bdo><ul id='ChcLK'></ul>

                          <tbody id='ChcLK'></tbody>

                        <legend id='ChcLK'><style id='ChcLK'><dir id='ChcLK'><q id='ChcLK'></q></dir></style></legend>
                          • 主站蜘蛛池模板: 久久久久久久国产精品影院 | 在线毛片网 | 日批免费在线观看 | 国产精品久久久 | 国产精品久久久久久久三级 | 日本免费一区二区三区四区 | 亚洲一区自拍 | 99久久精品免费看国产四区 | 亚洲高清视频在线 | 99久久精品免费 | 中文字幕亚洲一区 | 成人在线观看免费 | 久久久夜夜夜 | 欧美日韩大片 | 国产精品久久久久999 | 黄色网址免费看 | 精品一区二区三区在线观看 | 久久久91精品国产一区二区三区 | 久久一级免费视频 | 午夜寂寞影院在线观看 | 最新国产精品 | 91精品久久久 | 成人在线精品视频 | 欧美日产国产成人免费图片 | 91影院在线观看 | 天天干天天操天天看 | 一区二区三区在线观看视频 | 免费观看成人性生生活片 | www.99热| 中文字幕精品一区 | 亚洲va在线va天堂va狼色在线 | 亚洲欧美日韩中文在线 | 亚洲情侣视频 | 久久久久久免费毛片精品 | 国产不卡在线播放 | 欧美在线一区二区三区 | 97成人精品 | 日本在线视频中文字幕 | 性国产丰满麻豆videosex | 国产不卡一 | 精品国产一区二区三区免费 |