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

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

  • <small id='bGhPl'></small><noframes id='bGhPl'>

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

        如何模擬 @InjectMocks 類的方法?

        How can I mock methods of @InjectMocks class?(如何模擬 @InjectMocks 類的方法?)

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

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

                1. 本文介紹了如何模擬 @InjectMocks 類的方法?的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  例如我有處理程序:

                  @Component
                  public class MyHandler {
                  
                    @AutoWired
                    private MyDependency myDependency;
                  
                    public int someMethod() {
                      ...
                      return anotherMethod();
                    }
                  
                    public int anotherMethod() {...}
                  }
                  

                  為了測試它,我想寫這樣的東西:

                  to testing it I want to write something like this:

                  @RunWith(MockitoJUnitRunner.class}
                  class MyHandlerTest {
                  
                    @InjectMocks
                    private MyHandler myHandler;
                  
                    @Mock
                    private MyDependency myDependency;
                  
                    @Test
                    public void testSomeMethod() {
                      when(myHandler.anotherMethod()).thenReturn(1);
                      assertEquals(myHandler.someMethod() == 1);
                    }
                  }
                  

                  但每當(dāng)我嘗試模擬它時,它實(shí)際上都會調(diào)用 anotherMethod() .我應(yīng)該用 myHandler 做什么來模擬它的方法?

                  But it actually calls anotherMethod() whenever I try to mock it. What should I do with myHandler to mock its methods?

                  推薦答案

                  首先mock MyHandler方法的原因可能如下:我們已經(jīng)測試過anotherMethod(),它的邏輯很復(fù)雜,那么,如果我們可以verify 它正在調(diào)用,為什么我們需要再次測試它(就像 someMethod() 的一部分)?
                  我們可以通過:

                  First of all the reason for mocking MyHandler methods can be the following: we already test anotherMethod() and it has complex logic, so why do we need to test it again (like a part of someMethod()) if we can just verify that it's calling?
                  We can do it through:

                  @RunWith(MockitoJUnitRunner.class)
                  class MyHandlerTest {
                  
                    @Spy  
                    @InjectMocks  
                    private MyHandler myHandler;  
                  
                    @Mock  
                    private MyDependency myDependency;  
                  
                    @Test  
                    public void testSomeMethod() {  
                      doReturn(1).when(myHandler).anotherMethod();  
                      assertEquals(myHandler.someMethod() == 1);  
                      verify(myHandler, times(1)).anotherMethod();  
                    }  
                  }  
                  

                  注意:在 'spying' 對象的情況下,我們需要使用 doReturn 而不是 thenReturn(小解釋是 這里)

                  Note: in case of 'spying' object we need to use doReturn instead of thenReturn(little explanation is here)

                  這篇關(guān)于如何模擬 @InjectMocks 類的方法?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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)建一個隨機(jī)打亂數(shù)字的 int 數(shù)組)
                  Inconsistent behavior on java#39;s ==(java的行為不一致==)
                  Why is Java able to store 0xff000000 as an int?(為什么 Java 能夠?qū)?0xff000000 存儲為 int?)
                  <tfoot id='ReQ8p'></tfoot>

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

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

                            主站蜘蛛池模板: 黄色片av| 伊人免费在线观看高清 | 亚洲 欧美 另类 综合 偷拍 | 成人国产在线观看 | 国产欧美一区二区久久性色99 | 亚洲影音先锋 | 男人阁久久 | 成人久久视频 | 欧美久久久电影 | 夏同学福利网 | 日韩国产专区 | 亚洲国产成人久久久 | 久久一区精品 | 色久在线| 成人免费在线观看 | 九九99九九精彩46 | 日韩国产在线 | 亚洲一区二区黄 | 天堂一区| 青青草原综合久久大伊人精品 | 久久99网站| 99久久婷婷国产综合精品电影 | 国产三区在线观看视频 | 日韩欧美亚洲 | 亚洲日韩中文字幕 | 日本在线免费看最新的电影 | 国产在线精品一区二区三区 | 亚洲一区二区三区视频免费观看 | 日韩黄色小视频 | 一区二区在线免费观看视频 | 中文精品视频 | 午夜精品久久 | 成人精品视频在线观看 | 亚洲性人人天天夜夜摸 | 国产精品久久久久久久久久免费看 | 亚洲国产区| 黄色国产| 欧美八区 | 精品一区二区在线观看 | 亚洲大片| 亚洲91精品|