我有以下课程:
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!