Mockito.when(ApiCallImpl.invokeSubmitApplicationForm(Mockito.anyString(),Mockito.any(AppFormDetails.class))).thenReturn(AppFormSubmissionBOResponse.class.getResource("/appFormSubmission_BO_Resp.json"));
当我尝试嘲笑第三方服务时,我遇到了错误。
Cannot make a static reference to the non static method invokeSubmitApplicationForm(String, AppFormDetails) from the type ApiCallImpl
答案 0 :(得分:0)
invokeSubmitApplicationForm
是一个实例方法,因此需要在一个实例(在这种情况下为模拟实例)上调用:
ApiCallImpl api = Mockito.mock(ApiCallImpl.class);
Mockito.when(api.invokeSubmitApplicationForm
(Mockito.anyString(), Mockito.any(AppFormDetails.class))
).thenReturn(AppFormSubmissionBOResponse
.class
.getResource("/appFormSubmission_BO_Resp.json"));
答案 1 :(得分:0)
如果您正在使用PowerMockito,请在下面使用
PowerMockito.mockStatic(StaticClass.class);
PowerMockito.doReturn("WhatEverYouWant").when(StaticClass.class, "methodName", Mockito.anyString());
如果使用Mockito,则进行其他调整。
请不要忘记在@PrepareForTest
部分中包含静态类。
编辑:
call = Mockito.mock(ApiCallImpl.class);
Mockito.when(ApiCallImpl.invokeSubmitApplicationForm(Mockito.any(), Mockito.any()))
.thenReturn(AppFormSubmissionBOResponse.class.getResource("/appFormSubmission_BO_Resp.json"));
这足够了。
private ApiCallImpl call;
应该是声明。