喜逢一個我試圖嘲弄一個靜態方法名mapCreditInfo(UCIPin,creditAssessmentResults)它有兩個參數UCIPin和creditAssessmentResults。 UCIPin是字符串類型和creditAssessmentResults是列表 此方法是一個公共類ResponseMapper 類型內,如下所示:如何在使用Power mockito的公共類中模擬靜態方法?
private CreditInfo getAccountByUCI(String audiUser, String UCIPin) throws EnterpriseCustomerVerificationException {
List<CreditAssessmentResult> creditAssessmentResults = creditInfoRepository.getUCISummary(UCIPin, audiUser);
return ResponseMapper.mapCreditInfo(UCIPin, creditAssessmentResults);
}
Note: getAccountbyUCI method is called inside another public method name executeCustomerVerification which is in the class EnterpriseCustomerVerificationService
ResponseMapper類
}
我已經像我的測試課的一部分一樣試過了hown下面: 測試類
@PrepareForTest(EnterpriseCustomerVerificationService.class)
@RunWith(PowerMockRunner.class)
public class EnterpriseCustomerVerificationServiceTest {
@InjectMocks
private EnterpriseCustomerVerificationService enterpriseCustormerVerificationServiceMock ;
@Test
public void executeCustomerVerificationTest() throws Exception {
List<ErrorResponse> errorResponses = getErrorResponse();
List<String> mso = new ArrayList<String>();
mso.add("a");
mso.add("b");
mso.add("c");
AddressResponse addressResponse = getAddressResponse();
String experianAuthorization = "experianAuthorization";
String UCIPin = "110019";
String auditUser = "ABC";
CreditInfo credit = getCreditInfo();
CreditCheck creditCheck = getcreditCheck();
EnterpriseCustomerVerificationService spy = PowerMockito.spy(new EnterpriseCustomerVerificationService());
PowerMockito.when(spy,PowerMockito.method(EnterpriseCustomerVerificationService.class,"executeCreditCheck",CreditCheck.class)).withArguments(Mockito.any()).thenReturn("@1");
Mockito.when(creditInfoRepository.getUCISummary("110019", "test")).thenReturn(getCreditAssessmentResultList());
PowerMockito.mockStatic(ResponseMapper.class);
Mockito.when(ResponseMapper.mapCreditInfo(UCIPin, getCreditAssessmentResultList())).thenReturn(credit);
CustomerVerification cv = spy
.executeCustomerVerification(getCustomerVerificationRequest1(),
"101");
}
我的問題是如何嘲笑使用電力的Mockito 靜態mapCreditInfo方法?
感謝
評論你能告訴我們你有什麼到目前爲止已經試過?你已經寫了一些試圖嘲笑這種靜態方法的測試方法,所以也許你可以更新問題來包含該嘗試? – glytching
ok等待@ glitch –
@ glitch請檢查 –