使用JUnit重新初始化局部变量

时间:2016-04-04 21:37:40

标签: java junit

我正在为方法编写JUnit,我需要重新初始化ServiceContext以获得该行的完整分支覆盖。我不确定如何访问ServiceContext object,因为每次ctx为null时,都会向其中添加消息。有没有办法明确地访问它,以便我按照我想要的方式调整它?

待测方法:

public AgencyResponse getAgency(@QueryParam(ServiceConstants.AGENCY_CODE) String agencyCode,
        @QueryParam(ServiceConstants.EFFECTIVE_DATE) long effectiveDate) throws WebServiceException {
    ServiceContext ctx = getServiceContext();
    getBaseComponent().logMethodStartDebug(ctx, CLASS_NAME, "getAgency()" ); 

    AgencyResponse response = new AgencyResponse();

    try {
        if( effectiveDate < 1 ) {           
            effectiveDate = new Date().getTime();
        }

        ctx.addParameter( ServiceConstants.AGENCY_CODE, agencyCode );
        ctx.addParameter( ServiceConstants.EFFECTIVE_DATE, DateUtil.convertToDate( effectiveDate, false ) );

        validateRequest( ctx, response );

        IProviderChain providerChain = (IProviderChain)AppContext.getBean( ServiceConstants.AGENCY_PROVIDER_CHAIN );
        response = (AgencyResponse)providerChain.retrieveData( ctx );

        if( response == null ) { //precaution
            response = new AgencyResponse();
        }

        if( response.getAgency() == null ) {
            if(ctx.getMessages()==null || ctx.getMessages().isEmpty()){
                ctx.addMessage( ErrorMessageUtil.generateMessage( getBaseComponent().getMessageFactory(), ErrorCodeConstants.STATUS_2000 ) );
            }
        }

        return (AgencyResponse)finalizeResponse( response, ctx );
}

由于

0 个答案:

没有答案