我正在爲一個方法編寫一個JUnit測試用例,除了一羣setters之外,這個方法沒有業務邏輯。我正在測試它只是爲了使用Cobertura獲得更好的代碼覆蓋率。JUnit中的ClassCastException
我得到ClassCastException
,我不知道如何解決這個問題。我知道爲什麼會發生這種情況,但在這種特殊情況下,我不確定如何在JUnit代碼中處理它。
這是拋出異常的行。 dto.setValue(((String[])httpParams.get(ClientUserVendorParamConstants.DOCUSIGN_ACCOUNT_ID))[0]);
MUT
public IClientUserDto mapFieldsToTO(IClientUserDto clientUser, Map<String, Object> httpParams) {
ESignatureClientDetails details = SecurityUtils.getSecurityObject();
//Map Parameters
List<IClientUserVendorParamDto> params = new ArrayList<IClientUserVendorParamDto>();
IClientUserVendorParamDto dto = new ClientUserVendorParamDto();
dto.setName(ClientUserVendorParamConstants.DOCUSIGN_ACCOUNT_ID);
dto.setValue(((String[])httpParams.get(ClientUserVendorParamConstants.DOCUSIGN_ACCOUNT_ID))[0]);
dto.setCreatedBy(details.getUserIdentifier());
dto.setLastModifiedBy(details.getUserIdentifier());
params.add(dto);
dto = new ClientUserVendorParamDto();
dto.setName(ClientUserVendorParamConstants.DOCUSIGN_USER_NAME);
dto.setValue(((String[])httpParams.get(ClientUserVendorParamConstants.DOCUSIGN_USER_NAME))[0]);
dto.setCreatedBy(details.getUserIdentifier());
dto.setLastModifiedBy(details.getUserIdentifier());
params.add(dto);
dto = new ClientUserVendorParamDto();
dto.setName(ClientUserVendorParamConstants.DOCUSIGN_PASSWORD);
dto.setValue(((String[])httpParams.get(ClientUserVendorParamConstants.DOCUSIGN_PASSWORD))[0]);
dto.setCreatedBy(details.getUserIdentifier());
dto.setLastModifiedBy(details.getUserIdentifier());
params.add(dto);
clientUser.getClientUserVendor().setCreatedBy(details.getUserIdentifier());
clientUser.getClientUserVendor().setLastModifiedBy(details.getUserIdentifier());
clientUser.getClientUserVendor().setClientUserVendorParams(params);
return clientUser;
}
的JUnit
@Test
public void testMapFieldsToTO() throws Exception {
DocusignVendorDisplay dvd = new DocusignVendorDisplay();
IClientUserDto iClientUserDto = Mockito.mock(IClientUserDto.class);
ESignatureClientDetails clientDetails = Mockito.mock(ESignatureClientDetails.class);
PowerMockito.mockStatic(SecurityUtils.class);
PowerMockito.when(SecurityUtils.getSecurityObject()).thenReturn(clientDetails);
Map<String, Object> httpParams = new HashMap<>();
httpParams.put("AccountId", iClientUserDto);
iClientUserDto = dvd.mapFieldsToTO(iClientUserDto, httpParams);
}
如果你知道爲什麼發生異常,那麼也許你饒了我們搞清楚了這一點爲自己的麻煩?我的意思是,拼圖很棒,但Stack Exchange有一個單獨的網站。 –
我不明白爲什麼開發人員不理解這些問題。我知道ClassCast背後的原因,但我無法找出明智的解決方案代碼。我試過這個,但是這會拋出IndexOutofBounce。 Mockito.when(httpParams.get(Mockito.any(String.class)))。thenReturn(new String [] {}); – Jaykumar