Inject Mocks for objects created by Factory classes(为 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:
抽象工厂方法是最好的一个 - 这是一个隐藏实现细节的清晰设计
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为 Factory 类创建的对象注入 Mocks
基础教程推荐
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 降序排序:Java Map 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01