为 Factory 类创建的对象注入 Mocks

Inject Mocks for objects created by Factory classes(为 Factory 类创建的对象注入 Mocks)
本文介绍了为 Factory 类创建的对象注入 Mocks的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我有以下课程:

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

相关文档推荐

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