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

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

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

    <tfoot id='q5Ytf'></tfoot>
  1. <legend id='q5Ytf'><style id='q5Ytf'><dir id='q5Ytf'><q id='q5Ytf'></q></dir></style></legend>

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

    1. 如何使用 PowerMockito 模擬私有靜態方法?

      How can I mock private static method with PowerMockito?(如何使用 PowerMockito 模擬私有靜態方法?)

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

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

      <legend id='EnFIk'><style id='EnFIk'><dir id='EnFIk'><q id='EnFIk'></q></dir></style></legend>

          <tfoot id='EnFIk'></tfoot>
          • <bdo id='EnFIk'></bdo><ul id='EnFIk'></ul>
                  <tbody id='EnFIk'></tbody>

                本文介紹了如何使用 PowerMockito 模擬私有靜態方法?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                問題描述

                我正在嘗試模擬私有靜態方法 anotherMethod().見下面的代碼

                I'm trying to mock private static method anotherMethod(). See code below

                public class Util {
                    public static String method(){
                        return anotherMethod();
                    }
                
                    private static String anotherMethod() {
                        throw new RuntimeException(); // logic was replaced with exception.
                    }
                }
                

                這是我的測試代碼

                @PrepareForTest(Util.class)
                public class UtilTest extends PowerMockTestCase {
                
                        @Test
                        public void should_prevent_invoking_of_private_method_but_return_result_of_it() throws Exception {
                
                            PowerMockito.mockStatic(Util.class);
                            PowerMockito.when(Util.class, "anotherMethod").thenReturn("abc");
                
                            String retrieved = Util.method();
                
                            assertNotNull(retrieved);
                            assertEquals(retrieved, "abc");
                        }    
                }
                

                但是我運行的每一個圖塊都會出現這個異常

                But every tile I run it I get this exception

                java.lang.AssertionError: expected object to not be null
                

                我想我在嘲笑東西方面做錯了.有什么想法可以解決嗎?

                I suppose that I'm doing something wrong with mocking stuff. Any ideas how can I fix it?

                推薦答案

                為此,您可以使用 PowerMockito.spy(...)PowerMockito.doReturn(...).

                To to this, you can use PowerMockito.spy(...) and PowerMockito.doReturn(...).

                此外,您必須在測試類中指定 PowerMock 運行器,并準備測試類,如下所示:

                Moreover, you have to specify the PowerMock runner at your test class, and prepare the class for testing, as follows:

                @PrepareForTest(Util.class)
                @RunWith(PowerMockRunner.class)
                public class UtilTest {
                
                   @Test
                   public void testMethod() throws Exception {
                      PowerMockito.spy(Util.class);
                      PowerMockito.doReturn("abc").when(Util.class, "anotherMethod");
                
                      String retrieved = Util.method();
                
                      Assert.assertNotNull(retrieved);
                      Assert.assertEquals(retrieved, "abc");
                   }
                }
                

                希望對你有幫助.

                這篇關于如何使用 PowerMockito 模擬私有靜態方法?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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?)

              • <tfoot id='vx2vA'></tfoot>
                    <tbody id='vx2vA'></tbody>

                      <legend id='vx2vA'><style id='vx2vA'><dir id='vx2vA'><q id='vx2vA'></q></dir></style></legend>
                      • <bdo id='vx2vA'></bdo><ul id='vx2vA'></ul>

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

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

                        • 主站蜘蛛池模板: 日韩色在线 | 国产精品18久久久 | 欧美在线视频网站 | 国产精品毛片一区二区在线看 | 日韩和的一区二在线 | 欧美精品一区二区在线观看 | 亚洲入口 | 日韩在线观看中文字幕 | 欧美99 | 欧美电影大全 | 一区二区精品 | 久久精品小视频 | 国产精品一区二区免费 | 欧美11一13sex性hd | 亚洲欧美综合精品久久成人 | 欧美成人免费 | 国内久久 | 一区二区中文字幕 | 五月天国产在线 | 在线观看亚洲 | 国产精品免费一区二区三区四区 | 中文字幕电影在线观看 | 久久99精品国产99久久6男男 | 国产精品久久久久久妇女6080 | 亚洲高清一区二区三区 | 99视频在线免费观看 | 麻豆国产一区二区三区四区 | 欧美在线观看一区 | 国产一区二区三区网站 | 九九久久99| 日韩中文字幕久久 | 日韩欧美视频网站 | 日日摸日日爽 | 亚洲精品欧美一区二区三区 | 91av视频在线观看 | 精品国产乱码久久久久久久久 | 久久中文字幕一区 | 欧美一区二区三区高清视频 | 视频一区二区三区在线观看 | 日韩欧美成人精品 | 亚洲一区中文字幕 |