如何模拟接口 Java PowerMockito
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39267199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to mock An Interface Java PowerMockito
提问by Kikou
I m trying to mock an interface.
我正在尝试模拟一个界面。
public interface FlowCopyParamsBusinessManager {
List<FlowCopyParams> findByAppli(String application, String sourcePattern)
throws FlowCopyParamsBusinessException;
}
}
In my code, when i call this method findByAppli, i would like to return a list of FlowCopyParams.
在我的代码中,当我调用这个方法findByAppli 时,我想返回一个 FlowCopyParams 列表。
List<FlowCopyParams> lstFlowCopyParams = flowCopyParamsBusinessManager.findByAppli(
"TOTO","TATA);
Here my try in the class test:
这是我在课堂测试中的尝试:
@BeforeClass
public static void mockBeanIn() throws Exception {
List<FlowCopyParams> flowCopyParamsList = new ArrayList<>();
PowerMockito.spy(FlowCopyParamsBusinessManager.class);
PowerMockito.when(FlowCopyParamsBusinessManager.class, "findByAppli", Mockito.anyString(), Mockito.anyString()).thenReturn(flowCopyParamsList);
}
I have this error :
我有这个错误:
java.lang.IllegalArgumentException: object is not an instance of declaring class
I don't know why because the method findByAppli must have two string parameters, and i put Mockito.anyString()and i still have IllegalArgumentException.
我不知道为什么,因为 findByAppli 方法必须有两个字符串参数,我把Mockito.anyString()和我仍然有IllegalArgumentException。
Any clue ?
任何线索?
Thxs.
谢谢。
回答by UserF40
You don't need to use PowerMockito, and as its an Interface, theres no need to spy() as you are not relying on any non mocked logic.
您不需要使用 PowerMockito,并且作为它的接口,不需要 spy() 因为您不依赖任何非模拟逻辑。
It can be done like this, in your test class define a class variable.
可以这样做,在您的测试类中定义一个类变量。
private FlowCopyParamsBusinessManager flowCopyParamsBusinessManagerMock;
In an @Before
annotated method:
在带@Before
注释的方法中:
flowCopyParamsBusinessManagerMock = Mockito.mock(FlowCopyParamsBusinessManager.class);
List<FlowCopyParams> flowCopyParamsList = new ArrayList<>();
when(flowCopyParamsBusinessManagerMock
.findByAppli(Mockito.anyString(), Mockito.anyString()).thenReturn(flowCopyParamsList);
Then refer to flowCopyParamsBusinessManagerMock
in your tests.
然后flowCopyParamsBusinessManagerMock
在你的测试中参考。
回答by Kikou
My test did not work because I was trying to spy the class and not on the instance of FlowCopyParamsBusinessManager.class .
我的测试没有奏效,因为我试图监视该类而不是 FlowCopyParamsBusinessManager.class 的实例。
First , we have to create the mock :
首先,我们必须创建模拟:
FlowCopyParamsBusinessManager mockFlowCopyParamsBusinessManager = PowerMockito.mock(FlowCopyParamsBusinessManager.class);
Then , spy the instance :
然后,监视实例:
PowerMockito.spy(mockFlowCopyParamsBusinessManager);
PowerMockito.when(mockFlowCopyParamsBusinessManager, "findByAppli", Mockito.anyString(), Mockito.anyString()).thenReturn(flowCopyParamsList);
It works as well !
它也有效!
回答by abhishek ringsia
I did this put this @RunWith(PowerMockRunner.class) at the top of the class. then mock Object with PowerMockito.mock(MyMock.class); This way use can mock a interface or final class.
我这样做是把这个 @RunWith(PowerMockRunner.class) 放在班级的顶部。然后用 PowerMockito.mock(MyMock.class) 模拟对象;这种方式使用可以模拟接口或最终类。