久久久久久久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 對(duì)象

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

      <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 對(duì)象的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                問題描述

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

                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í)行如此棘手的注入,因?yàn)樗皇且粋€(gè)注入框架.因此,您需要重構(gòu)代碼以使其更具可測(cè)試性.使用構(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;
                    }
                }
                

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

                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 對(duì)象的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

                相關(guān)文檔推薦

                How can I detect integer overflow on 32 bits int?(如何檢測(cè) 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. <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一区二区 | 干干干操操操 | 亚洲精品一区二区三区中文字幕 | 亚洲精品久久久久avwww潮水 | 精品国产乱码久久久久久丨区2区 | 99在线免费观看视频 | 中文字字幕在线中文乱码范文 | 久久久精 | 一区二区三区亚洲 | 精品成人免费一区二区在线播放 | 成人做爰69片免费观看 | 欧美五月婷婷 | 最新国产精品视频 | 亚洲精品99 | 5060网一级毛片| 午夜av电影| 国产亚洲一区二区三区 | 激情婷婷 | 欧美黄视频| 亚州综合在线 | 欧美日韩精品免费观看 | 一级欧美日韩 | 日日摸夜夜添夜夜添精品视频 | 一区二区精品 | 97色在线视频 | 精品国产乱码久久久久久中文 | 在线午夜电影 | www.日韩系列 | 麻豆va| 成人免费看黄网站在线观看 | 中文成人在线 | 欧美一区二区在线视频 | 久久久91 | 亚洲视频在线免费观看 | 欧美最猛黑人xxxⅹ 粉嫩一区二区三区四区公司1 | 亚洲精品电影在线观看 | 视频一区二区三区中文字幕 | 国产一区二区观看 | 国产精品免费观看 |