私は、セッターの束を除いてビジネスロジックを持たないメソッドのためのJUnitテストケースを書いています。私は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);
}
例外が発生した理由を知っている場合は、おそらくあなたは私たちに自分のためにそれを考え出すの手間を惜しまだろうか?つまり、パズルは素晴らしく、すべてですが、スタックエクスチェンジには別のサイトがあります。 –
私は開発者が質問を理解していない理由を理解していません。 ClassCastの背後にある理由は分かっていますが、ソリューションコードを賢明に理解することはできません。私はこれを試みましたが、これはIndexOutofBounceをスローします。 \t Mockito.when(httpParams.get(Mockito.any(String.class)))then return(new String [] {}); – Jaykumar