2016-09-19 33 views
-2

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

} 
+0

如果你知道为什么发生异常,那么也许你饶了我们搞清楚了这一点为自己的麻烦?我的意思是,拼图很棒,但Stack Exchange有一个单独的网站。 –

+0

我不明白为什么开发人员不理解这些问题。我知道ClassCast背后的原因,但我无法找出明智的解决方案代码。我试过这个,但是这会抛出IndexOutofBounce。 Mockito.when(httpParams.get(Mockito.any(String.class)))。thenReturn(new String [] {}); – Jaykumar

回答

0

我假设 ClientUserVendorParamConstants.DOCUSIGN_ACCOUNT_ID"AccountId"

,并要为"AccountId"设置 Mockito.mock(IClientUserDto.class);

但将其作为StringArray ((String[])httpParams.get(ClientUserVendorParamConstants.DOCUSIGN_ACCOUNT_ID))

由于您没有提供足够的信息,因此我们很难理解并理解这些信息。例如,我们不知道dto(IClientUserVendorParamDto)是什么,并且dto.setValue是假设设置的。 它是否设置IClientUserDto或字符串?

至于我的答案,你为什么不尝试分离这一行

dto.setValue(((String[])httpParams.get(ClientUserVendorParamConstants.DOCUSIGN_ACCOUNT_ID))[0]);

这样

String[] accountIdArray = httpParams.get(ClientUserVendorParamConstants.DOCUSIGN_ACCOUNT_ID)); 
String accountId = accountIdArray[0]: 
dto.setValue(accountId); 

,看看哪条线路发生ClassCastException异常。

0

我想通了。谢谢大家的回应。我必须将字符串转换为字符串数组,我将调用模拟方法。

更正代码:

@Test 
public void testMapFieldsToTO() throws Exception { 
    DocusignVendorDisplay dvd = new DocusignVendorDisplay(); 
    IClientUserDto iClientUserDto = Mockito.mock(IClientUserDto.class); 
    ESignatureClientDetails clientDetails = Mockito.mock(ESignatureClientDetails.class); 

    String[] accountId = new String[]{"AccountId"}; 

    PowerMockito.mockStatic(SecurityUtils.class); 
    PowerMockito.when(SecurityUtils.getSecurityObject()).thenReturn(clientDetails); 

    Map<String, Object> httpParams = Mockito.mock(Map.class); 

    Mockito.when(((String[])httpParams.get(Mockito.any(String.class)))).thenReturn(accountId); 

    iClientUserDto = dvd.mapFieldsToTO(iClientUserDto, httpParams); 

} 
相关问题