<legend id='9Ug4K'><style id='9Ug4K'><dir id='9Ug4K'><q id='9Ug4K'></q></dir></style></legend>

<small id='9Ug4K'></small><noframes id='9Ug4K'>

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

      1. <tfoot id='9Ug4K'></tfoot>

        • <bdo id='9Ug4K'></bdo><ul id='9Ug4K'></ul>
      2. 待测类中的模拟类

        时间:2023-09-25

          <bdo id='cVGFS'></bdo><ul id='cVGFS'></ul>
            <legend id='cVGFS'><style id='cVGFS'><dir id='cVGFS'><q id='cVGFS'></q></dir></style></legend>

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

                  <tbody id='cVGFS'></tbody>
                <tfoot id='cVGFS'></tfoot>

                <i id='cVGFS'><tr id='cVGFS'><dt id='cVGFS'><q id='cVGFS'><span id='cVGFS'><b id='cVGFS'><form id='cVGFS'><ins id='cVGFS'></ins><ul id='cVGFS'></ul><sub id='cVGFS'></sub></form><legend id='cVGFS'></legend><bdo id='cVGFS'><pre id='cVGFS'><center id='cVGFS'></center></pre></bdo></b><th id='cVGFS'></th></span></q></dt></tr></i><div id='cVGFS'><tfoot id='cVGFS'></tfoot><dl id='cVGFS'><fieldset id='cVGFS'></fieldset></dl></div>
                • 本文介绍了待测类中的模拟类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  如何在我正在测试的班级中使用 Mockito 模拟其他班级?

                  How I can mock with Mockito other classes in my class which is under test?

                  例如:

                  MyClass.java

                  MyClass.java

                  class MyClass {
                      public boolean performAnything() {
                          AnythingPerformerClass clazz = new AnythingPerformerClass();
                          return clazz.doSomething();        
                      }
                  }
                  

                  AnythingPerformerClass.java

                  AnythingPerformerClass.java

                  class AnythingPerformerClass {
                      public boolean doSomething() {
                          //very very complex logic
                          return result;
                      }
                  }
                  

                  并测试:

                  @Test
                  public void testPerformAnything() throws Exception {
                      MyClass clazz = new MyClass();
                      Assert.assertTrue(clazz.performAnything());
                  }
                  

                  我可以欺骗 AnythingPerformerClass 以从 AnythingPerformerClass 中排除不必要的逻辑吗?我可以覆盖 doSomething() 方法以简单返回 truefalse 吗?

                  Can I spoof AnythingPerformerClass for excluding unnecessary logic from AnythingPerformerClass? Can I override doSomething() method for simple return true or false?

                  为什么我指定 Mockito,因为我需要它来使用 Robolectric 进行 Android 测试.

                  Why I specify Mockito, because I need it for Android testing with Robolectric.

                  推荐答案

                  你可以重构 MyClass 让它使用 依赖注入.您可以将类的实例传递给 MyClass 的构造函数,而不是让它创建一个 AnythingPerformerClass 实例,如下所示:

                  You could refactor MyClass so that it uses dependency injection. Instead of having it create an AnythingPerformerClass instance you could pass in an instance of the class to the constructor of MyClass like so :

                  class MyClass {
                  
                     private final AnythingPerformerClass clazz;
                  
                     MyClass(AnythingPerformerClass clazz) {
                        this.clazz = clazz;
                     }
                  
                     public boolean performAnything() {         
                       return clazz.doSomething();        
                     }
                  }
                  

                  然后你可以在单元测试中传入模拟实现

                  You can then pass in the mock implementation in the unit test

                  @Test
                  public void testPerformAnything() throws Exception {
                     AnythingPerformerClass mockedPerformer = Mockito.mock(AnythingPerformerClass.class);
                     MyClass clazz = new MyClass(mockedPerformer);
                     ...
                  }
                  

                  或者,如果您的 AnythingPerformerClass 包含状态,那么您可以将 AnythingPerformerClassBuilder 传递给构造函数.

                  Alternatively, if your AnythingPerformerClass contains state then you could pass a AnythingPerformerClassBuilder to the constructor.

                  这篇关于待测类中的模拟类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Mockito - 注入模拟列表 下一篇:将泛型与 Mockito 匹配

                  相关文章

                  最新文章

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

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

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

                      • <bdo id='QJTfQ'></bdo><ul id='QJTfQ'></ul>
                      <tfoot id='QJTfQ'></tfoot>