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

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

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

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

      <tfoot id='zs3jX'></tfoot>

      Mockito 將模擬注入 Spy 對象

      Mockito Inject mock into Spy object(Mockito 將模擬注入 Spy 對象)

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

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

            • <tfoot id='Izjmg'></tfoot>
                <tbody id='Izjmg'></tbody>
              • <i id='Izjmg'><tr id='Izjmg'><dt id='Izjmg'><q id='Izjmg'><span id='Izjmg'><b id='Izjmg'><form id='Izjmg'><ins id='Izjmg'></ins><ul id='Izjmg'></ul><sub id='Izjmg'></sub></form><legend id='Izjmg'></legend><bdo id='Izjmg'><pre id='Izjmg'><center id='Izjmg'></center></pre></bdo></b><th id='Izjmg'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='Izjmg'><tfoot id='Izjmg'></tfoot><dl id='Izjmg'><fieldset id='Izjmg'></fieldset></dl></div>
                本文介紹了Mockito 將模擬注入 Spy 對象的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                問題描述

                我正在為具有 2 級依賴注入的類編寫測試用例.我對 1 級依賴注入對象使用 @Spy 注釋,我想模擬第 2 級注入.但是,我在第二級不斷收到空指針異常.有什么方法可以將模擬注入@Spy 對象?

                I'm writing a test case for a Class which has a 2 level of dependency injection. I use @Spy annotation for the 1 level dependency injection object, and I would like to Mock the 2nd level of injection. However, I kept getting null pointer exception on the 2nd level. Is there any way that I inject the mock into the @Spy object?

                public class CarTestCase{
                    @Mock
                    private Configuration configuration;
                
                    @Spy 
                    private Engine engine;
                
                    @InjectMocks 
                    private Car car;
                
                    @Test
                    public void test(){
                
                       Mockito.when(configuration.getProperties("")).return("Something");
                       car.drive();
                    }
                
                }
                
                public class Car{
                    @Inject
                    private Engine engine;
                
                    public void drive(){
                        engine.start();
                    }
                }
                
                public class Engine{
                    @Inject 
                    private Configuration configuration;
                
                    public void start(){
                        configuration.getProperties();   // null pointer exception
                    }
                
                }
                

                推薦答案

                Mockito 不能執(zhí)行如此棘手的注入,因為它不是一個注入框架.因此,您需要重構(gòu)代碼以使其更具可測試性.使用構(gòu)造函數(shù)注入很容易做到:

                Mockito cannot perform such a tricky injections as it's not an injection framework. So, you need to refactor your code to make it more testable. It's easy done by using constructor injection:

                public class Engine{
                    private Configuration configuration;
                
                    @Inject 
                    public Engine(Configuration configuration) {
                        this.configuration = configuration;
                    }
                    ........
                }
                
                public class Car{
                    private Engine engine;
                
                    @Inject    
                    public Car(Engine engine) {
                        this.engine = engine;
                    }
                }
                

                在這種情況下,您必須手動處理模擬和注入:

                In this case you have to handle the mocking and injection manually:

                public class CarTestCase{
                
                    private Configuration configuration;
                
                    private Engine engine;
                
                    private Car car;
                
                    @Before
                    public void setUp(){
                        configuration = mock(Configuration.class);
                        engine = spy(new Engine(configuration));
                        car = new Car(engine);
                    }
                
                    @Test
                    public void test(){
                
                       Mockito.when(configuration.getProperties("")).return("Something");
                       car.drive();
                    }
                
                }
                

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

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

                      • <tfoot id='5AtYz'></tfoot>
                          <legend id='5AtYz'><style id='5AtYz'><dir id='5AtYz'><q id='5AtYz'></q></dir></style></legend>
                          主站蜘蛛池模板: 看片网站在线 | 一区二区在线 | 天天操天天舔 | 成人精品鲁一区一区二区 | 国产精品视频一区二区三区四区国 | 中文字幕 在线观看 | 国产亚洲精品综合一区 | 久久久久亚洲视频 | 成人午夜在线 | 成人在线视频网站 | 国产在线精品一区二区三区 | 黄色一级大片在线免费看产 | 欧美区在线 | 狠狠涩 | 老子午夜影院 | 国产精品视频久久 | 日韩国产在线 | 超碰人人91 | 国产欧美日韩综合精品一区二区 | 国产在线1 | 91久久精品日日躁夜夜躁欧美 | 麻豆一区二区三区精品视频 | 亚洲精品一区av在线播放 | 日韩在线视频一区二区三区 | 欧美成人一区二免费视频软件 | 草久在线视频 | 日韩精品一区二区三区免费观看 | 欧美综合国产精品久久丁香 | 夜夜骑天天干 | 欧美精品成人一区二区三区四区 | 夜夜夜久久久 | 日韩免费视频一区二区 | 亚洲国产区 | 精品国产一区久久 | 美女爽到呻吟久久久久 | 成人av一区 | 99爱视频 | 无人区国产成人久久三区 | 99视频在线 | 无人区国产成人久久三区 | 久久九九色 |