我想嘲笑我的class Filter
:Filter filter = Mockito.spy(new Filter());
在Filter
的构造函数中(这是我的问题):
public Filter() {
this.gameConfig = FactoryProvider.of().getConfigFactory().createGameConfig();
}
FactoryProvider
是工厂的工厂。静态方法 {of()
返回FactoryProvider
的实例。
getConfigFactory()
返回ConfigFactory
的实例
并且createGameConfig()
返回GameConfig
我的想法是这个:
public class FilterTest {
@Spy
FactoryProvider factoryProvider;
@Before
public void setUp() {
when(FactoryProvider.of()).thenReturn(factoryProvider);
when(factoryProvider.getConfigFactory()).thenReturn(new FakeFactory());
}
public static class FakeFactory extends ConfigFactory {
public GameConfig createGameConfig() {
return new GameConfig();
}
}
@Test
public myTest() {
Filter filter = Mockito.spy(new Filter());
/////////////////
失败,并且出现此错误:
java.lang.RuntimeException: Invoking the beforeTestMethod method on PowerMock test listener org.powermock.api.extension.listener.AnnotationEnabler@13d73fa failed.
at org.powermock.tests.utils.impl.PowerMockTestNotifierImpl.notifyBeforeTestMethod(PowerMockTestNotifierImpl.java:84)