JUnit中的ClassCastException

时间:2016-09-19 21:22:39

标签: java junit

我正在为一个没有业务逻辑的方法编写一个JUnit测试用例,除了一堆setter。我正在测试它只是为了使用Cobertura获得更好的代码覆盖率。

我收到了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);

}

2 个答案:

答案 0 :(得分:0)

我在假设  ClientUserVendorParamConstants.DOCUSIGN_ACCOUNT_ID"AccountId"

你正在设置  Mockito.mock(IClientUserDto.class);的{​​{1}},

但是将其作为StringArray "AccountId"

我们理解事情是非常困惑和困难的,因为你没有向我们提供足够的信息。例如,我们不知道dto(((String[])httpParams.get( ClientUserVendorParamConstants.DOCUSIGN_ACCOUNT_ID )))是什么,并且IClientUserVendorParamDto是设置的。 是否设置IClientUserDto或String?

但至于我的答案,为什么不试着分开这一行

dto.setValue

像这样

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

并查看ClassCastException出现的行。

答案 1 :(得分:0)

我明白了。谢谢大家的回复。我必须将String转换为String of Array,我在这里调用mocked方法。

更正代码:

@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);

}