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

        <tfoot id='bsWl0'></tfoot>
          <bdo id='bsWl0'></bdo><ul id='bsWl0'></ul>

        为 Factory 类创建的对象注入 Mocks

        时间:2023-09-25
        <tfoot id='36j5S'></tfoot>
            <tbody id='36j5S'></tbody>

          <small id='36j5S'></small><noframes id='36j5S'>

          • <bdo id='36j5S'></bdo><ul id='36j5S'></ul>

                  <legend id='36j5S'><style id='36j5S'><dir id='36j5S'><q id='36j5S'></q></dir></style></legend>
                1. <i id='36j5S'><tr id='36j5S'><dt id='36j5S'><q id='36j5S'><span id='36j5S'><b id='36j5S'><form id='36j5S'><ins id='36j5S'></ins><ul id='36j5S'></ul><sub id='36j5S'></sub></form><legend id='36j5S'></legend><bdo id='36j5S'><pre id='36j5S'><center id='36j5S'></center></pre></bdo></b><th id='36j5S'></th></span></q></dt></tr></i><div id='36j5S'><tfoot id='36j5S'></tfoot><dl id='36j5S'><fieldset id='36j5S'></fieldset></dl></div>
                  本文介绍了为 Factory 类创建的对象注入 Mocks的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有以下课程:

                  public class MyClass {        
                      private Apple apple;
                  
                      public void myMethod() {
                         apple = AppleFactory.createInstance(someStringVariable);
                         ....
                         ....
                         ....
                      }
                  }
                  

                  还有测试类:

                  @RunWith(MockitoJUnitRunner.class)
                  public class MyClassTest {
                  
                          @InjectMocks 
                          MyClass myClass;
                  
                          @Test
                          public void myMethod(){
                           ...
                           ...
                           ...
                          }
                      }
                  

                  如何在 MyClass 中注入 Apple 实例作为模拟?

                  How could I inject an Apple instance as a mock in MyClass?

                  推荐答案

                  你有 3 种可能解决这个问题:

                  You have 3 possibilities to solve this:

                  抽象工厂:不要使用静态方法,而是使用具体工厂类:

                  Abstract factory: Instead of using a static method, use a concrete factory class:

                  public abstract class AppleFactory {
                      public Apple createInstance(final String str);
                  }
                  
                  public class AppleFactoryImpl implements AppleFactory {
                      public Apple createInstance(final String str) { // Implementation }
                  }
                  

                  在您的测试类中,模拟工厂:

                  In your test class, mock the factory:

                  @RunWith(MockitoJUnitRunner.class)
                  public class MyClassTest {
                  
                      @Mock
                      private AppleFactory appleFactoryMock;
                  
                      @Mock
                      private Apple appleMock;
                  
                      @InjectMocks 
                      MyClass myClass;
                  
                      @Before
                      public void setup() {
                          when(appleFactoryMock.createInstance(Matchers.anyString()).thenReturn(appleMock);
                      }
                  
                      @Test
                      public void myMethod(){
                       ...
                       ...
                       ...
                      }
                  }
                  

                  PowerMock:使用 PowerMock 创建静态方法的模拟.查看我对相关问题的回答了解它是如何完成的.

                  PowerMock: Use PowerMock to create a mock of a static method. Look at my answer to a relevant question to see how it's done.

                  可测试类:将 Apple 创建包装在 protected 方法中,并创建一个覆盖它的测试类:

                  Testable class: Make the Apple creation wrapped in a protected method and create a test class that overrides it:

                  public class MyClass {
                     private Apple apple;
                  
                     public void myMethod() {
                         apple = createApple();
                         ....
                         ....
                         ....
                     }
                  
                     protected Apple createApple() {
                         return AppleFactory.createInstance(someStringVariable);
                     }
                  }
                  
                  
                  @RunWith(MockitoJUnitRunner.class)
                  public class MyClassTest {
                  
                      @Mock
                      private Apple appleMock;
                  
                      @InjectMocks 
                      MyClass myClass;
                  
                      @Test
                      public void myMethod(){
                       ...
                       ...
                       ...
                      }
                  
                      private class TestableMyClass extends MyClass {
                         @Override
                         public void createApple() {
                            return appleMock;
                         }
                      }
                  }
                  

                  当然,在你的测试类中你应该测试 TestableMyClass 而不是 MyClass.

                  Of course, in your test class you should test TestableMyClass and not MyClass.

                  我会告诉你我对每种方法的看法:

                  I'll tell you my opinion on each of the methods:

                  1. 抽象工厂方法是最好的一个 - 这是一个隐藏实现细节的清晰设计

                  1. The abstract factory method is the best one - This is a clear design that hides the implementation details

                  可测试类 - 是第二个需要最少更改的选项

                  The testable class - Is the second option which requires minimum changes

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

                  上一篇:你如何模拟 JavaFX 工具包初始化? 下一篇:Mockito - 文件的模拟行为

                  相关文章

                  最新文章

                  1. <small id='TfoBz'></small><noframes id='TfoBz'>

                    <tfoot id='TfoBz'></tfoot>
                    <legend id='TfoBz'><style id='TfoBz'><dir id='TfoBz'><q id='TfoBz'></q></dir></style></legend>

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

                      • <bdo id='TfoBz'></bdo><ul id='TfoBz'></ul>