我正在为一个方法编写一个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