EasyMock在一种方法中处理多个异常

时间:2014-07-10 07:37:44

标签: java easymock

在我的代码中,我在此BusinesserviceException的Catch块中发现了一个BusinessServiceException.And,它抛出另一个RestValidationException。我无法处理此RestValidation异常。我得到的错误是意外的RestValidationException。 请帮忙......

try {
        if(null == identityProxy.getIdentityProxy() || null == identityProxy.getIdentityProxyType() || identityProxy.getIdentityProxy().isEmpty() || identityProxy.getIdentityProxyType().isEmpty()){
            throw new RestValidationException(RestErrorCodes.INVALID_IDENTITY_PROXY.toString());
        }
        restValidations.validIdentityProxy(identityProxy.getIdentityProxy(),identityProxy.getIdentityProxyType());
        if(userHelper.addUserIdentityProxy(partnerId, userId,identityProxy.getIdentityProxy(),identityProxy.getIdentityProxyType(), null, 30L,true)) {
            successResponseVO.setSuccess(RESTConstants.TRUE);
        } else {
            successResponseVO.setSuccess(RESTConstants.FALSE);
        }
    } catch (BusinessServiceException be) {
        try{
             if(be.getNewInformationCode().equalsIgnoreCase(BusinessErrorCodes.UNIQUE_PROXY_PHONE_ERROR.toString())){
                    throw new RestValidationException(RestErrorCodes.VALIDATION_UNIQUE_PHONE_PROXY.toString());}
                 else if(be.getNewInformationCode().equalsIgnoreCase(BusinessErrorCodes.UNIQUE_PROXY_EMAIL_ERROR.toString())){
                     throw new RestValidationException(RestErrorCodes.VALIDATION_UNIQUE_EMAIL_PROXY.toString());
                 }else if(be.getNewInformationCode().equalsIgnoreCase(BusinessErrorCodes.UNIQUE_PROXY_LOYALTY_ERROR.toString())){
                     throw new RestValidationException(RestErrorCodes.VALIDATION_UNIQUE_LOYALTY_PROXY.toString());
                 }else{
                     throw new BusinessServiceException(partnerId, "Error in UserRestController : updateUser()", be, be.getNewInformationCode(), params);
                 } 
            }catch(RestValidationException ve){
                throw new RestValidationException(partnerId, "Error in UserRestController : updateUser()", ve, ve.getNewInformationCode(), params);
            }

        }catch (RestValidationException ve){
            throw new RestValidationException(partnerId, "Error in UserRestController : updateUser()", ve, ve.getNewInformationCode(), params);
        }  catch (Exception e) {
            throw new RestInternalErrorException(partnerId, "Error in UserRestController : updateUser()", e, null, params);
        } 

    return successResponseVO;
}

EasyMock代码:

   @Test(expected = BusinessServiceException.class)
    public void testUpdateUserDetailBusinessServiceExceptionEmail() throws BusinessException, RestValidationException,RestInternalErrorException {

        UserDetailVO userDetailVO = new UserDetailVO();
        UserDetail userDetail = new UserDetail();
        userDetail.setPhoneNumber("04828229279");
        userDetail.setEmail("divya@gmail.com");
        String deliveryType = "deliveryType";
        List<UserDeliveryPreference> userDeliveryPreference = new ArrayList<UserDeliveryPreference>();
        UserDeliveryPreference e = new UserDeliveryPreference();
        e.setDeliveryType(deliveryType);
        userDeliveryPreference.add(e);
        userDetail.setUserDeliveryPreference(userDeliveryPreference);
        userDetail.setSendDigitalReceipt("never");
        UserEvent userEvent = new UserEvent();
        userEvent.setEventType(EventsType.UserPreferenceUpdate.toString());
        userEvent.setPartnerId(partnerId);
        userEvent.setUserId(userId);
        userEvent.setTimestamp(new Date());
        userEvent.setChannel(Channel.rest.toString());
        EasyMock.expect(request.getHeader("x-forwarded-for")).andReturn("x-forwarded-for");
        EasyMock.expect(request.getHeader("user-agent")).andReturn("user-agent");
        Map<String, String> details = new HashMap<String, String>();
        details.put(RESTConstants.IP_ADDRESS, "x-forwarded-for");
        details.put(RESTConstants.HTTP_USER_AGENT, "user-agent");
        details.put(RESTConstants.APPID, "4567");
        userEvent.setDetails(details);
        EasyMock.expect(restValidations.validateDeliveryPreference(userDetailVO)).andReturn(true);
        EasyMock.expect(userHelper.populateuserDetail(userDetailVO)).andReturn(userDetail);
        EasyMock.expect( restValidations.isAllRequiredFieldExistsForUserUpdate(userDetail)).andReturn(true);
        EasyMock.expect(restValidations.validatePhonePattern("04828229279")) .andReturn(true);
        EasyMock.expect(restValidations.validateEmailPattern("divya@gmail.com")).andReturn(true);
        EasyMock.expect( restValidations.validDeliveryPreference(userDeliveryPreference)) .andReturn(true);
        EasyMock.expect(restValidations.validateOptin("never")).andReturn("never");
        String newInformationCode = BusinessErrorCodes.UNIQUE_PROXY_EMAIL_ERROR.toString();
        Serializable[] params = null;
        try {
            EasyMock.expect(userHelper.updateUserDetail(EasyMock.same(userId),EasyMock.same(partnerId),EasyMock.isA(UserDetail.class),EasyMock.isA(UserEvent.class))).andThrow( new BusinessServiceException(partnerId, "fails", null, newInformationCode, params));
            EasyMock.replay(userHelper, request, restValidations);
            userController.updateUser(partnerId, userId, "4567", "1.0", "nonce", userDetailVO, request);
        } finally {
            EasyMock.verify(userHelper, request, restValidations);
        }
    }

1 个答案:

答案 0 :(得分:0)

看来您的测试期望抛出错误的异常类型。

变化:

@Test(expected = BusinessServiceException.class)
public void testUpdate(parameters){

为:

@Test(expected = RestValidationException.class)
public void testUpdate(parameters){