久久久久久久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 為參數(shù)化構(gòu)造函數(shù)創(chuàng)

        why cannot we create spy for Parameterized Constructor using Mockito(為什么我們不能使用 Mockito 為參數(shù)化構(gòu)造函數(shù)創(chuàng)建間諜)

      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 為參數(shù)化構(gòu)造函數(shù)創(chuàng)建間諜的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  我的代碼中只有參數(shù)化的構(gòu)造函數(shù),我需要通過它進(jìn)行注入.

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

                  我想監(jiān)視參數(shù)化構(gòu)造函數(shù)以注入模擬對象作為我的 junit 的依賴項(xiàng).

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

                  但是我們有什么東西可以讓我在構(gòu)造函數(shù)中注入模擬對象并監(jiān)視它嗎?

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

                  推薦答案

                  您可以通過在 junit 中使用參數(shù)化構(gòu)造函數(shù)實(shí)例化您的主類,然后從中創(chuàng)建一個(gè)間諜來做到這一點(diǎn).

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

                  假設(shè)您的主類是A.其中 BC 是它的依賴項(xiàng)

                  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;
                      }
                  }
                  

                  然后您可以使用下面的參數(shù)化類為此編寫 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 是您使用參數(shù)化構(gòu)造函數(shù)注入到您的類 A 中的模擬對象.
                  2. 然后我們創(chuàng)建了一個(gè)名為 spyAAspy.
                  3. 我們通過修改類 A 中受保護(hù)方法 method2 的返回值來檢查 spy 是否真的有效,這不可能如果 spyA 不是 A 的實(shí)際 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.

                  這篇關(guān)于為什么我們不能使用 Mockito 為參數(shù)化構(gòu)造函數(shù)創(chuàng)建間諜的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

                  相關(guān)文檔推薦

                  How can I detect integer overflow on 32 bits int?(如何檢測 32 位 int 上的整數(shù)溢出?)
                  Local variables before return statements, does it matter?(return 語句之前的局部變量,這有關(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?)

                    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>
                          • 主站蜘蛛池模板: av网站在线看 | 亚洲电影成人 | 中文视频在线 | 国产色黄| 日韩精品一二三 | 欧美一级免费 | 在线免费观看成人 | 日韩高清在线 | 国产精品久久久久久久久久免费看 | 888久久久 | 狠狠操在线 | 日韩国产中文字幕 | 伊人春色成人网 | 91亚洲精华国产 | 中文字幕在线观看第一页 | 日韩和的一区二区 | 91精品国产91久久综合桃花 | 国内精品视频免费观看 | 亚洲欧美精品久久 | 日韩欧美在线观看视频网站 | 激情久久网| 狠狠草视频 | 久久久久久久国产 | 久久成人精品视频 | 国产综合精品 | 亚洲福利精品 | 成人区精品一区二区婷婷 | 一级特黄网站 | av网址在线 | 亚洲视频1区 | 成人h片在线观看 | 中文字幕乱码一区二区三区 | 久久久精品网站 | 欧美日韩视频 | 国产亚洲成av人片在线观看桃 | 一区二区三区四区在线视频 | 国产成人精品区一区二区不卡 | 精品国产乱码久久久久久影片 | 日韩久久精品 | 在线观看亚洲专区 | 国产欧美精品一区二区三区 |