<legend id='P1spo'><style id='P1spo'><dir id='P1spo'><q id='P1spo'></q></dir></style></legend>
  • <tfoot id='P1spo'></tfoot>
    1. <i id='P1spo'><tr id='P1spo'><dt id='P1spo'><q id='P1spo'><span id='P1spo'><b id='P1spo'><form id='P1spo'><ins id='P1spo'></ins><ul id='P1spo'></ul><sub id='P1spo'></sub></form><legend id='P1spo'></legend><bdo id='P1spo'><pre id='P1spo'><center id='P1spo'></center></pre></bdo></b><th id='P1spo'></th></span></q></dt></tr></i><div id='P1spo'><tfoot id='P1spo'></tfoot><dl id='P1spo'><fieldset id='P1spo'></fieldset></dl></div>

        <bdo id='P1spo'></bdo><ul id='P1spo'></ul>

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

        Mockito 将模拟注入 Spy 对象

        Mockito Inject mock into Spy object(Mockito 将模拟注入 Spy 对象)
        <i id='y0dfC'><tr id='y0dfC'><dt id='y0dfC'><q id='y0dfC'><span id='y0dfC'><b id='y0dfC'><form id='y0dfC'><ins id='y0dfC'></ins><ul id='y0dfC'></ul><sub id='y0dfC'></sub></form><legend id='y0dfC'></legend><bdo id='y0dfC'><pre id='y0dfC'><center id='y0dfC'></center></pre></bdo></b><th id='y0dfC'></th></span></q></dt></tr></i><div id='y0dfC'><tfoot id='y0dfC'></tfoot><dl id='y0dfC'><fieldset id='y0dfC'></fieldset></dl></div>
      1. <tfoot id='y0dfC'></tfoot>

          <bdo id='y0dfC'></bdo><ul id='y0dfC'></ul>
              <tbody id='y0dfC'></tbody>

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

                • <legend id='y0dfC'><style id='y0dfC'><dir id='y0dfC'><q id='y0dfC'></q></dir></style></legend>
                  本文介绍了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 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
                  How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
                  Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
                  Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
                  How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
                  How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)

                  <legend id='huXOC'><style id='huXOC'><dir id='huXOC'><q id='huXOC'></q></dir></style></legend>
                      <tbody id='huXOC'></tbody>

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

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

                          <bdo id='huXOC'></bdo><ul id='huXOC'></ul>