JMock:方法的意外调用

时间:2012-04-25 10:14:52

标签: java junit

我尝试搜索论坛但找不到适合我问题的解决方案。

我的代码是:

public class ClassTest {

    /** The context. */
    private Mockery context = new Mockery();

    /** The mapper service. */
    private MapperService mapperService;

    @Before
    public void init() {
        mapperService = context.mock(MapperService.class);
    }

    @Test
    public void testUpdateRequestStatusToSuccessful()
    {
        final RequestQueue inRequestQueue = new RequestQueue();
        final String finalOutputZipFileName = "abc.txt";
        final Mapper mapperEntityObj = new Mapper();
        mapperEntityObj.setAutoMarkSuccessful(true);
        mapperEntityObj.setMarkRecordsAsExported(true);

        MapperTypeReference mapperType=new MapperTypeReference();
        mapperType.setCode("C");
        mapperEntityObj.setMapperType(mapperType);

        DataSetTypeReference datasetType=new DataSetTypeReference();
        datasetType.setCode("TA");
        mapperEntityObj.setDataSet(datasetType);

        inRequestQueue.setRowCount(15);
        inRequestQueue.setId(25128002L);

        context.checking(new Expectations() {
        {
            allowing(referenceDataService).retrieveReferenceCodeByCode(RequestStatus.class, "L");
            will(returnValue(new RequestStatus()));

            allowing(referenceDataService).retrieveReferenceCodeByCode(RequestStatus.class, "N");
            will(returnValue(new RequestStatus()));

            allowing(requestQueueDao).persist(with(any(RequestQueue.class)));

            one(mapperService).persist(mapperEntityObj);

            allowing(referenceDataService).retrieveReferenceCodeByCode(RequestStatus.class, "A");
            will(returnValue(new RequestStatus()));

        }
    });

    // Row count > 0; MarkRecordsAsExported(true)
    deuDataAccessServiceImpl.updateRequestStatusToSuccessful(inRequestQueue, finalOutputZipFileName, mapperEntityObj);
}

我的服务类代码是

protected void updateRequestStatusToSuccessful(RequestQueue inRequestQueue,
        String finalOutputZipFileName, Mapper mapperEntityObj) {

    boolean mapperChanged = false;

    logger.info("updateRequestStatusToSuccessful(" + 
            "inRequestQueue, finalOutputZipFileName, mapperEntityObj) - begin");
    int rowCount=inRequestQueue.getRowCount();

    RequestStatus requestStatusToSet = null;

    logger.info("updateRequestStatusToSuccessful(" + 
            "inRequestQueue, finalOutputZipFileName, mapperEntityObj) - requestQueue: " + inRequestQueue);

    // Added to change the status of the request
    Boolean autoMarkSuccessful = mapperEntityObj.getAutoMarkSuccessful();
    Boolean markRecordsAsExported = mapperEntityObj.getMarkRecordsAsExported();
    if(rowCount==0){
        logger.info("No data found from dataSetKeys: Will set RequestStatus to No data found");
        requestStatusToSet = referenceDataService
                                                .retrieveReferenceCodeByCode(RequestStatus.class,
                                                RequestStatusEnum.NODATAFOUND.getCode());
        if(markRecordsAsExported){
            // Unlocking a mapper and setting the lastSuccessfulEndDate if successful
            mapperEntityObj.setIsLocked(false);
            mapperChanged = true;
        }
    }else if(markRecordsAsExported){
        logger.info("Mark transactions as exported enabled: Will set RequestStatus based on autoMarkSuccesful");
        if(autoMarkSuccessful){
            logger.info("autoMarkSuccesful enabled: Will set RequestStatus to Successful");
            requestStatusToSet = referenceDataService
                                                    .retrieveReferenceCodeByCode(RequestStatus.class,
                                                    RequestStatusEnum.SUCCESSFUL.getCode());
        } else {
            logger.info("autoMarkSuccesful disabled: Will set RequestStatus to Awaiting");
            requestStatusToSet = referenceDataService
                                                    .retrieveReferenceCodeByCode(RequestStatus.class,
                                                    RequestStatusEnum.AWAITING.getCode());
        }
    } else {
        logger.info("Mark transactions as exported disabled: Will set RequestStatus to Successful");
        requestStatusToSet = referenceDataService
                                            .retrieveReferenceCodeByCode(RequestStatus.class,
                                            RequestStatusEnum.SUCCESSFUL.getCode());
    }


    logger.info("Setting requestQueueStatus to : " + requestStatusToSet.getCode());
    inRequestQueue.setRequestStatus(requestStatusToSet);

    if (finalOutputZipFileName != null) {
        inRequestQueue.setOutputFilePathText(finalOutputZipFileName);
    }


    Date currentTimeStamp = Calendar.getInstance().getTime();
    // Values to set RequestQueueStartedTimestamp
    inRequestQueue.setCompletedTimestamp(currentTimeStamp);

    // Request queue object to persist
    logger.info("updateRequestStatusToSuccessful(" + 
            "inRequestQueue, finalOutputZipFileName, mapperEntityObj) - requestQueue: " + inRequestQueue);
    requestQueueDao.persist(inRequestQueue);
    // End of added to change the status
    logger.info("updateRequestStatusToSuccessful(" + 
            "inRequestQueue, finalOutputZipFileName, mapperEntityObj) - end");

    // PRD296 - FAST DEU
    logger.info("Before check to set mapper last successful end date.");
    if (((MapperTypesEnum.CUSTOMMAPPER.getCode().equals(mapperEntityObj.getMapperType().getCode()) 
            && DeuUtil.isTransactionBasedMapper(mapperEntityObj.getDataSet().getCode())) 
            || MapperTypesEnum.FLEXMAPPER.getCode().equals(mapperEntityObj.getMapperType().getCode()))
            && (autoMarkSuccessful || !markRecordsAsExported)
        ){
        logger.info("Set Last Successful end date: Copied Over Mapper End Date [" + mapperEntityObj.getMapperEndDate() + "]");  
        mapperEntityObj.setLastSuccessfulEndDate(mapperEntityObj.getMapperEndDate());
        mapperChanged = true;
    }
    else {
        logger.info("Mapper last successful end date not set: Mapper end date = " + mapperEntityObj.getMapperEndDate() 
                + ", Mapper type = " + mapperEntityObj.getMapperType().getCode() 
                + ", Mapper dataset code = "+ mapperEntityObj.getDataSet().getCode()
                + ", Is transaction based mapper = "
                + DeuUtil.isTransactionBasedMapper(mapperEntityObj.getDataSet().getCode()));
    }

    // Optimization
    if(mapperChanged){
        mapperService.persist(mapperEntityObj);
    }
}

但是我仍然得到了mapperService.persists的意外调用。

任何人都可以帮助我吗?

0 个答案:

没有答案