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

                問題描述

                我正在為具有 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>
                          主站蜘蛛池模板: 黄色小视频免费看 | 一本色道久久综合亚洲精品酒店 | 黄色在线小视频 | 黄色网av | 丁香午夜| 久久久久久久成人 | 免费视频国产 | 日韩欧美亚洲国产 | 黄色成人免费网站 | 黄色片观看 | 97福利视频 | 国产免费无遮挡 | 久久久夜 | 在线观看黄色小视频 | 午夜在线影院 | 在线视频亚洲 | 久久久久久久97 | 他揉捏她两乳不停呻吟动态图 | 亚洲免费福利视频 | 午夜天堂在线 | 日本久久网 | 久久精品视频一区二区 | 国产一区在线观看视频 | 一级片av| www.日日日| 亚洲一区网站 | 草草免费视频 | 欧美一级日韩一级 | 免费在线观看黄色片 | 一级毛片国产 | 一级二级毛片 | 日本在线视频一区 | 九九久久精品视频 | 成人av一区二区三区在线观看 | 91精品国产成人www | 久久性生活视频 | 在线一区二区视频 | 国产一区二区在线观看视频 | 欧美日韩一区二区三区四区 | 黄色小视频免费观看 | 一级黄色录相 |