/**
 * Bestpay.com.cn Inc.
 * Copyright (c) 2011-2016 All Rights Reserved.
 */
package com.bestpay.bpbp.bill.core.coreService;

import com.bestpay.bpbp.bill.core.base.BaseSpringTest;
import com.bestpay.bpbp.bill.core.facade.dto.core.BaseCoreResult;
import com.bestpay.bpbp.bill.core.facade.dto.core.request.QueryOrderRequest;
import com.bestpay.bpbp.bill.core.facade.dto.core.request.QueryRefundRequest;
import com.bestpay.bpbp.bill.core.service.service.QueryOrderService;
import com.bestpay.bpbp.common.context.OperationContext;
import lombok.extern.slf4j.Slf4j;
import org.easymock.EasyMock;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

import static org.easymock.EasyMock.*;

/**
 * EasyMock使用案例
 * @author lwh
 * @version Id: QueryOrderServiceMockTest.java, v 0.1 2016/02/16 15:32 lwh Exp $$
 *
 * 这个过程大致可以划分为以下几个步骤:
 *      1、使用 EasyMock 生成 Mock 对象
 *      2、设定 Mock 对象的预期行为和输出
 *      3、将 Mock 对象切换到 Replay 状态
 *      4、调用 Mock 对象方法进行单元测试
 *      5、对 Mock 对象的行为进行验证
 */


@Slf4j
public class QueryOrderServiceMockTest extends BaseSpringTest {

    @Autowired
    private static QueryOrderService queryOrderService;

    @BeforeClass
    public static void initEasyMock(){
        queryOrderService  =  createMock(QueryOrderService. class );
        System.out.println(queryOrderService.toString());
        System.out.println("-------------------");
    }
    //设定 Mock 对象的预期行为和输出
    public  void doMock(){
        BaseCoreResult res = new BaseCoreResult();
        reset(queryOrderService); // 重置mock对象
        expect(queryOrderService.queryBpbpOrder(EasyMock.anyObject(QueryOrderRequest.class),EasyMock.anyObject(OperationContext.class))).andReturn(res); // (b)
        replay(queryOrderService); //结束录制过程,将 Mock 对象切换到 Replay 状态
    }

    /**
     * 查询销账单
     */
    @Test
    public void testQueryBpbpOrder(){
        doMock();//调用 Mock 对象方法进行单元测试
        OperationContext operationContext = new OperationContext();
        operationContext.setTime(new Date());
        operationContext.setAppIp("123.213.123.132");
        operationContext.setAppName("ticketApp");
        QueryOrderRequest queryOrderCoreRequest = new QueryOrderRequest();
        queryOrderCoreRequest.setBpbpOrderNo("2015112812413210033820");
        queryOrderCoreRequest.setUserAccount("15813350170");
        queryOrderCoreRequest.setBizType("TRAIN_TICKET");
        queryOrderCoreRequest.setChannelType("CLIENT");
        queryOrderCoreRequest.setPhoneNo("15626252388");
        queryOrderCoreRequest.setRequestId("123456");
        queryOrderCoreRequest.setSupplierCode("tn");
        BaseCoreResult baseCoreResult = queryOrderService.queryBpbpOrder(queryOrderCoreRequest,operationContext);
        verify(queryOrderService); // 检查 trainTicketService 是否如预期一样调用了 queryTrainTicket 方法。对 Mock 对象的行为进行验证
        log.info("baseCoreResult:{}",baseCoreResult);

    }

}

EasyMock

   1、添加EesyMock的maven依赖
       

            org.easymock

            easymock

            3.0

            test

       

    2、使用 EasyMock 生成 Mock 对象

    

    @BeforeClass

    public  void initEasyMock(){

        trainTicketService  =  createMock(TrainTicketService. class );

    }

    3、设定 Mock 对象的预期行为和输出

    public  void doMock(BusinessDataRequest businessDataRequest){

        CommonResponse> res = new CommonResponse>();

        res.setRespCode("039999");

        res.setRespDesc("certificateTypeName字段不能为空");

        res.setResult(null);

        reset(trainTicketService); // 重置mock对象

        expect(trainTicketService.queryTrainTicket(businessDataRequest, null)).andReturn(res); // (b)

        replay(trainTicketService); //结束录制过程

    }

    4、调用 Mock 对象方法进行单元测试

        doMock(businessDataRequest);

        CommonResponse> respon = trainTicketService

                .queryTrainTicket(businessDataRequest, null);

      5、对 Mock 对象的行为进行验证

         verify(trainTicketService); // 检查 trainTicketService 是否如预期一样调用了queryTrainTicket 方法。