<legend id='Izjmg'><style id='Izjmg'><dir id='Izjmg'><q id='Izjmg'></q></dir></style></legend>
    • <bdo id='Izjmg'></bdo><ul id='Izjmg'></ul>
  1. <small id='Izjmg'></small><noframes id='Izjmg'>

    1. <tfoot id='Izjmg'></tfoot>
    2. <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 id='Izjmg'><tfoot id='Izjmg'></tfoot><dl id='Izjmg'><fieldset id='Izjmg'></fieldset></dl></div>

      Mockito 将模拟注入 Spy 对象

      时间:2023-10-01

          <tbody id='zs3jX'></tbody>
        • <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 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 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我正在为具有 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 不能执行如此棘手的注入,因为它不是一个注入框架.因此,您需要重构代码以使其更具可测试性.使用构造函数注入很容易做到:

                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();
                    }
                
                }
                

                这篇关于Mockito 将模拟注入 Spy 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何使用 Mockito 测试 DAO 方法? 下一篇:多个级别的@Mock 和@InjectMocks

                相关文章

                最新文章

                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 id='5AtYz'><tfoot id='5AtYz'></tfoot><dl id='5AtYz'><fieldset id='5AtYz'></fieldset></dl></div>
                    <bdo id='5AtYz'></bdo><ul id='5AtYz'></ul>

                2. <tfoot id='5AtYz'></tfoot>
                    <legend id='5AtYz'><style id='5AtYz'><dir id='5AtYz'><q id='5AtYz'></q></dir></style></legend>