繁体   English   中英

一种方法的EasyMock处理多个异常

[英]EasyMock Handling of Multiple Exceptions in one method

在我的代码中,我遇到了一个BusinessServiceException。在此BusinesserviceException的Catch块中,它抛出了另一个RestValidationException。我无法处理此RestValidation Exception。我遇到的错误是意外的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);
        }
    }

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

更改:

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

至:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM