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

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

        <tfoot id='B2EMZ'></tfoot>

        为什么我们不能使用 Mockito 为参数化构造函数创建间谍

        why cannot we create spy for Parameterized Constructor using Mockito(为什么我们不能使用 Mockito 为参数化构造函数创建间谍)
            • <tfoot id='fHcpM'></tfoot>

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

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

                  本文介绍了为什么我们不能使用 Mockito 为参数化构造函数创建间谍的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我的代码中只有参数化的构造函数,我需要通过它进行注入.

                  I have only parameterized constructor in my code and i need to inject through it.

                  我想监视参数化构造函数以注入模拟对象作为我的 junit 的依赖项.

                  I want to spy parameterized constructor to inject mock object as dependency for my junit.

                  public RegDao(){
                   //original object instantiation here
                  Notification ....
                  EntryService .....
                  }
                  
                  public RegDao(Notification notification , EntryService entry) {
                   // initialize here
                  }
                  
                  we have something like below : 
                  RegDao dao = Mockito.spy(RegDao.class);
                  

                  但是我们有什么东西可以让我在构造函数中注入模拟对象并监视它吗?

                  But do we have something that i can inject mocked object in the Constructor and spy it?.

                  推荐答案

                  您可以通过在 junit 中使用参数化构造函数实例化您的主类,然后从中创建一个间谍来做到这一点.

                  You can do that by instantiating your main class with parametrized constructor in your junit and then creating a spy from it.

                  假设您的主类是A.其中 BC 是它的依赖项

                  Let's suppose your main class is A. Where B and C are its dependencies

                  public class A {
                  
                      private B b;
                  
                      private C c;
                  
                      public A(B b,C c)
                      {
                          this.b=b;
                          this.c=c;
                      }
                  
                      void method() {
                          System.out.println("A's method called");
                          b.method();
                          c.method();
                          System.out.println(method2());
                  
                      }
                  
                      protected int method2() {
                          return 10;
                      }
                  }
                  

                  然后您可以使用下面的参数化类为此编写 junit

                  Then you can write junit for this using your parametrized class as below

                  @RunWith(MockitoJUnitRunner.class)
                  public class ATest {
                  
                      A a;
                  
                      @Mock
                      B b;
                  
                      @Mock
                      C c;
                  
                      @Test
                      public void test() {
                          a=new A(b, c);
                          A spyA=Mockito.spy(a);
                  
                          doReturn(20).when(spyA).method2();
                  
                          spyA.method();
                      }
                  }
                  

                  测试类的输出

                  A's method called
                  20
                  

                  1. 这里 BC 是您使用参数化构造函数注入到您的类 A 中的模拟对象.
                  2. 然后我们创建了一个名为 spyAAspy.
                  3. 我们通过修改类 A 中受保护方法 method2 的返回值来检查 spy 是否真的有效,这不可能如果 spyA 不是 A 的实际 spy.
                  1. Here B and C are mocked object that you injected in your class A using parametrized constructor.
                  2. Then we created a spy of A called spyA.
                  3. We checked if spy is really working by modifying the return value of a protected method method2 in class A which could not have been possible if spyA was not an actual spy of A.

                  这篇关于为什么我们不能使用 Mockito 为参数化构造函数创建间谍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 中的默认语言环境设置以使其保持一致?)

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

                        <tfoot id='hpT95'></tfoot>
                      • <small id='hpT95'></small><noframes id='hpT95'>

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

                          1. <legend id='hpT95'><style id='hpT95'><dir id='hpT95'><q id='hpT95'></q></dir></style></legend>
                              <tbody id='hpT95'></tbody>