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

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

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

        <tfoot id='Vu9aY'></tfoot>
        <legend id='Vu9aY'><style id='Vu9aY'><dir id='Vu9aY'><q id='Vu9aY'></q></dir></style></legend>
        <i id='Vu9aY'><tr id='Vu9aY'><dt id='Vu9aY'><q id='Vu9aY'><span id='Vu9aY'><b id='Vu9aY'><form id='Vu9aY'><ins id='Vu9aY'></ins><ul id='Vu9aY'></ul><sub id='Vu9aY'></sub></form><legend id='Vu9aY'></legend><bdo id='Vu9aY'><pre id='Vu9aY'><center id='Vu9aY'></center></pre></bdo></b><th id='Vu9aY'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='Vu9aY'><tfoot id='Vu9aY'></tfoot><dl id='Vu9aY'><fieldset id='Vu9aY'></fieldset></dl></div>
      1. 使用 Mockito 在另一個類中模擬一個類方法

        Using Mockito to mock a class method inside another class(使用 Mockito 在另一個類中模擬一個類方法)

          <small id='99Sbe'></small><noframes id='99Sbe'>

            <bdo id='99Sbe'></bdo><ul id='99Sbe'></ul>

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

              <legend id='99Sbe'><style id='99Sbe'><dir id='99Sbe'><q id='99Sbe'></q></dir></style></legend>
                • <tfoot id='99Sbe'></tfoot>
                  本文介紹了使用 Mockito 在另一個類中模擬一個類方法的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我正在嘗試使用 Mockito/JUnit 為這樣的函數(shù)編寫單元測試:

                  I'm trying to write unit tests with Mockito / JUnit for a function like this:

                  class1 {
                   method {
                    object1 = class2.method // method that I want to fake the return value
                    // some code that I still want to run
                   }
                  }
                  

                  在 Mockito 中有什么方法可以存根 class2.method 的結果嗎?我正在嘗試提高 class1 的代碼覆蓋率,因此我需要調(diào)用它的實際生產(chǎn)方法.

                  Is there any way in Mockito to stub the result of class2.method? I'm trying to improve code coverage for class1 so I need to call its real production methods.

                  我查看了 Mockito API 的 spy 方法,但這會覆蓋整個方法,而不是我想要的部分.

                  I looked into the Mockito API at its spy method but that would overwrite the whole method and not the part that I want.

                  推薦答案

                  我想我理解你的問題.讓我重新表述一下,您有一個正在嘗試測試的函數(shù),并且想要模擬在該函數(shù)中調(diào)用的函數(shù)的結果,但在不同的類中.我已經(jīng)通過以下方式處理了.

                  I think I am understanding your question. Let me re-phrase, you have a function that you are trying to test and want to mock the results of a function called within that function, but in a different class. I have handled that in the following way.

                  public MyUnitTest {
                      private static final MyClass2 class2 = mock(MyClass2.class);
                  
                      @Begin
                      public void setupTests() {
                          when(class2.get(1000)).thenReturn(new User(1000, "John"));
                          when(class2.validateObject(anyObj()).thenReturn(true);
                      }
                  
                      @Test
                      public void testFunctionCall() {
                          String out = myClass.functionCall();
                          assertThat(out).isEqualTo("Output");
                      }
                  }
                  

                  這樣做是在使用 @Before 注釋包裝的函數(shù)中,我正在設置我希望 class2 中的函數(shù)如何響應給定的特定輸入.然后,在實際測試中,我只是在我想要測試的類中調(diào)用我試圖測試的函數(shù).在這種情況下,myClass.functionCall() 正常運行,您不會覆蓋它的任何方法,而只是模擬它從 MyClass2 中的方法(或方法)獲得的輸出.

                  What this is doing is that within the function wrapped with the @Before annotation, I am setting up how I want the functions in class2 to respond given specific inputs. Then, from within the actual test, I am just calling the function that I am trying to test in the class I want to test. In this case, the myClass.functionCall() is running through as normal and you are not overwriting any of its methods, but you are just mocking the outputs that it gets from the methods (or method) within MyClass2.

                  這篇關于使用 Mockito 在另一個類中模擬一個類方法的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

                  相關文檔推薦

                  How can I detect integer overflow on 32 bits int?(如何檢測 32 位 int 上的整數(shù)溢出?)
                  Local variables before return statements, does it matter?(return 語句之前的局部變量,這有關系嗎?)
                  How to convert Integer to int?(如何將整數(shù)轉換為整數(shù)?)
                  How do I create an int array with randomly shuffled numbers in a given range(如何在給定范圍內(nèi)創(chuàng)建一個隨機打亂數(shù)字的 int 數(shù)組)
                  Inconsistent behavior on java#39;s ==(java的行為不一致==)
                  Why is Java able to store 0xff000000 as an int?(為什么 Java 能夠?qū)?0xff000000 存儲為 int?)

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

                            <tbody id='cZLMk'></tbody>

                          <tfoot id='cZLMk'></tfoot>

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

                          • 主站蜘蛛池模板: 久久精品国产一区二区电影 | 九七午夜剧场福利写真 | 天天操欧美 | 久久久久久国产精品久久 | 国产一区二区精品在线 | 欧美激情欧美激情在线五月 | 人人草人人干 | 国产在线a | 国产精品女人久久久 | 91成人在线视频 | 激情婷婷 | 亚洲成人精品国产 | 日日淫 | 亚洲伦理自拍 | 亚洲精品免费视频 | 欧美1—12sexvideos| 日韩精品在线免费 | 亚洲一二三区在线观看 | av国产精品毛片一区二区小说 | 亚洲国产日韩欧美 | 91视频在线看 | 91最新在线视频 | 久久爱综合| 欧美 日韩 中文 | 精品99爱视频在线观看 | 久操av在线| 精品久久香蕉国产线看观看亚洲 | 久久午夜视频 | 亚洲国产小视频 | 日韩激情视频一区 | a级黄色片在线观看 | 最新午夜综合福利视频 | 成年人在线视频 | 日韩三级电影一区二区 | 美国一级片在线观看 | 亚洲精品一区二三区不卡 | 日韩中文字幕视频 | 色视频免费 | 亚洲激精日韩激精欧美精品 | 亚洲视频在线免费观看 | 久久综合伊人一区二区三 |