写UT:使用Junit写UT

3 篇文章 1 订阅

在此之前我们需要弄清楚,在什么情况下使用mock写UT,在什么情况下使用Junit写UT。
大多数情况来说,不需要与数据库发生数据交互的使用mock,即;与数据库发生交互的使用Junit写UT,即增、删、改
当然还有特殊情况:逻辑很复杂,代码量很多,重构很彻底的一些功能方法使用Junit是很容易出错的,场景太多,无法全部覆盖到,我觉得应该使用mock测试比较好。功能重构之后=A模块+B模块,A有4种场景,B有三种场景,若使用mock,只需要模拟9重场景,而使用Junit,需要模拟4*5=20种场景。

一、Junit基类

以下给出了一个dao层的例子,我们只对其增、删、改部分使用Junit测试。

/**
 * Junit测试基类
 * 
 * @author Shuai Chen
 * 2018年12月17日   上午10:47:38
 *
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = BzSpringBootApplication.class)
@ActiveProfiles("dev")
public class BaseSpringBootTest {
    @Autowired
    //这里使用MongoTemplate是因为
    protected MongoTemplate mongoTemplate; 
    
    @Test
    public void 测试() { 
        
    }
}

二、被测试的类

@Log4j2
@Repository
public class InvoiceDaoImpl extends BaseDao implements InvoiceDao {

    @Override
    public void insertTrade(Trade trade) {
        getMongoTemplate().insert(trade);
    }

    @Override
    public void pushInvoice(Trade trade) {
        Query query = Query.query(Criteria.where(COL_ORDER_NO).is(trade.getOrderNo()));
        getMongoTemplate().updateFirst(query, getUpdatePushInvoice(trade.getInvoiceList()), Trade.class);
    }

    @Override
    public Integer updateRedBlueInvoice(Trade trade) {
        Criteria criteria = Criteria.where(COL_SHOP_CODE).is(trade.getShopCode());
        criteria.and(COL_ORDER_NO).is(trade.getOrderNo());
        criteria.and(COL_VERSION).is(trade.getVersion());
        Query query = new Query(criteria);

        Update update = newUpdate(trade);
        update.set(COL_INVOICE_LIST, trade.getInvoiceList());

        WriteResult writeResult = getMongoTemplate().updateFirst(query, update, Trade.class);
        return writeResult.getN();
    }

    @Override
    public void updateInvoiceStatus(Trade trade) {
        Invoice invoice = trade.getInvoiceList().iterator().next();

        Criteria criteria = Criteria.where(COL_SHOP_CODE).is(trade.getShopCode());
        criteria.and(QUERY_INVOICE_SERIA_NO).is(invoice.getSeriaNo());
        Query query = new Query(criteria);

        Update update = newUpdate(trade);

        update.set(COL_LAST_INVOICE_MODIFY_TIME, invoice.getLastModifyTime());
        update.set(COL_STATUS, invoice.getStatus());
        update.set(COL_ERROR_MSG, invoice.getErrorMsg());

        getMongoTemplate().updateFirst(query, update, Trade.class);
    }

    @Override
    public void updateInvoiceResult(Trade trade) {
        Invoice invoice = trade.getInvoiceList().iterator().next();
        Criteria criteria = new Criteria();
        criteria.and(QUERY_INVOICE_SERIA_NO).is(invoice.getSeriaNo());
        Query query = new Query(criteria);

        Update update = newUpdate(trade);
        if(Objects.nonNull(invoice.getLastModifyTime())){
            update.set(COL_LAST_INVOICE_MODIFY_TIME, invoice.getLastModifyTime());
        }
        if(Objects.nonNull(invoice.getStatus())){
            update.set(COL_STATUS, invoice.getStatus());
        }
        if(Objects.nonNull(invoice.getErrorMsg())){
            update.set(COL_ERROR_MSG, invoice.getErrorMsg());
        }
        if(Objects.nonNull(invoice.getInvoiceCode())){
            update.set(COL_INVOICE_CODE, invoice.getInvoiceCode());
        }
        if(Objects.nonNull(invoice.getInvoiceNo())){
            update.set(COL_INVOICE_NO, invoice.getInvoiceNo());
        }
        if(Objects.nonNull(invoice.getCheckCode())){
            update.set(COL_CHECK_CODE, invoice.getCheckCode());
        }
        if(Objects.nonNull(invoice.getInvoiceUrl())){
            update.set(COL_INVOICE_URL, invoice.getInvoiceUrl());
        }
        if(Objects.nonNull(invoice.getInvoiceApproveInfo())
            &&Objects.nonNull(invoice.getInvoiceApproveInfo().getOaRequestId())){
            update.set(COL_OA_REQUEST_ID, invoice.getInvoiceApproveInfo().getOaRequestId());
        }

        getMongoTemplate().updateFirst(query, update, Trade.class);
    }

    @Override
    public Boolean existsTrade(String orderNo) {
        return getMongoTemplate().exists(new Query(Criteria.where(COL_ORDER_NO).is(orderNo)), Trade.class);
    }

    @Override
    public Boolean existsItemId(String orderNo, String itemId) {
        Criteria criteria = Criteria.where(COL_ORDER_NO).is(orderNo);
        criteria.and(QUERY_INVOICE_ITEM_ID).is(itemId);
        return getMongoTemplate().exists(new Query(criteria), Trade.class);
    }

    @Override
    public List<InvoiceDO> findNotReversedBlueInvoice(RedBlueInvoiceCreateDTO redInvoiceCreateTrade) {
        List<AggregationOperation> aggregationOperationList = Lists.newArrayList();
        aggregationOperationList.add(Aggregation.unwind(MongoDbColumnConstants.COL_INVOICE_LIST));
        Criteria criteria = new Criteria();
        criteria.and(MongoDbColumnConstants.COL_SHOP_CODE).is(redInvoiceCreateTrade.getShopCode());
        criteria.and(MongoDbColumnConstants.COL_ORDER_NO).is(redInvoiceCreateTrade.getPfDocNo());
        criteria.and(MongoDbColumnConstants.QUERY_INVOICE_STATUS).ne(Constants.INVOICE_STATUS_CANCEL);
        criteria.and(MongoDbColumnConstants.QUERY_RED_OR_BLUE).is(Constants.INVOICE_BLUE);
        criteria.and(MongoDbColumnConstants.QUERY_IS_REVERSED).is(Constants.REVERSED_NO);
        aggregationOperationList.add(Aggregation.match(criteria));

        return aggregateList(aggregationOperationList, Trade.class, InvoiceDO.class);
    }

    @Override
    public List<InvoiceDO> findNeedReversedBlueInvoice(RedBlueInvoiceCreateDTO redInvoiceCreateTrade, Set<String> reversedSeriaNoSet) {
        List<AggregationOperation> aggregationOperationList = Lists.newArrayList();
        aggregationOperationList.add(Aggregation.unwind(MongoDbColumnConstants.COL_INVOICE_LIST));
        Criteria criteria = new Criteria();
        criteria.and(MongoDbColumnConstants.COL_SHOP_CODE).is(redInvoiceCreateTrade.getShopCode());
        criteria.and(MongoDbColumnConstants.COL_ORDER_NO).is(redInvoiceCreateTrade.getPfDocNo());
        criteria.and(MongoDbColumnConstants.QUERY_INVOICE_STATUS).ne(Constants.INVOICE_STATUS_CANCEL);
        criteria.and(MongoDbColumnConstants.QUERY_INVOICE_SERIA_NO).in(new ArrayList<>(reversedSeriaNoSet));
        criteria.and(MongoDbColumnConstants.QUERY_RED_OR_BLUE).is(Constants.INVOICE_BLUE);
        aggregationOperationList.add(Aggregation.match(criteria));

        return this.aggregateList(aggregationOperationList, Trade.class, InvoiceDO.class);
    }

    @Override
    public List<InvoiceDO> findNotIssuedElectronicInvoiceList(String shopCode, Integer size) {
        List<AggregationOperation> aggregationOperationList = Lists.newArrayList();
        aggregationOperationList.add(Aggregation.unwind(COL_INVOICE_LIST));
        Criteria criteria = new Criteria();
        criteria.and(COL_SHOP_CODE).is(shopCode);
        criteria.and(COL_INVOICE_CARRIER).is(Constants.INVOICE_CARRIER_ELECTRONIC);
        criteria.and(QUERY_INVOICE_STATUS).is(Constants.INVOICE_STATUS_NOT_SEND);
        aggregationOperationList.add(Aggregation.match(criteria));
        aggregationOperationList.add(Aggregation.skip(0L));
        aggregationOperationList.add(Aggregation.limit(size));

        return this.aggregateList(aggregationOperationList, Trade.class, InvoiceDO.class);
    }

    @Override
    public List<InvoiceDO> findInvoiceByOrderNoStatus(String shopCode, String orderNo, Integer status) {
        List<AggregationOperation> aggregationOperationList = new ArrayList<>();
        aggregationOperationList.add(Aggregation.unwind(COL_INVOICE_LIST));

        Criteria criteria = Criteria.where(COL_SHOP_CODE).is(shopCode);
        criteria.and(COL_ORDER_NO).is(orderNo);
        criteria.and(QUERY_INVOICE_STATUS).ne(status);
        criteria.and(QUERY_IS_REVERSED).is(Constants.REVERSED_NO);
        aggregationOperationList.add(Aggregation.match(criteria));

        return this.aggregateList(aggregationOperationList, Trade.class, InvoiceDO.class);
    }

    @Override
    public Integer findNotFinishedRedInvoiceCount(String shopCode, String orderNo) {
        Criteria criteria = Criteria.where(COL_SHOP_CODE).is(shopCode);
        criteria.and(COL_ORDER_NO).is(orderNo);
        criteria.and(QUERY_INVOICE_RED_OR_BLUE).is(Constants.INVOICE_RED);
        List<Integer> statusList = new ArrayList<>();
        statusList.add(Constants.INVOICE_STATUS_CREATE);
        statusList.add(Constants.INVOICE_STATUS_NOT_SEND);
        statusList.add(Constants.INVOICE_STATUS_SENDING);
        statusList.add(Constants.INVOICE_STATUS_FAIL);
        criteria.and(MongoDbColumnConstants.QUERY_INVOICE_STATUS).in(statusList);

        List<AggregationOperation> aggregationOperationList = Lists.newArrayList();
        aggregationOperationList.add(Aggregation.unwind(COL_INVOICE_LIST));
        aggregationOperationList.add(Aggregation.match(criteria));

        return aggregateCount(aggregationOperationList, Trade.class);
    }

    @Override
    public Long findFinishedBlueInvoice(String shopCode, String originalSeriaNo) {
        Criteria criteria = Criteria.where(COL_SHOP_CODE).is(shopCode);
        criteria.and(QUERY_INVOICE_RED_OR_BLUE).is(Constants.INVOICE_BLUE);
        criteria.and(QUERY_INVOICE_ORIGINAL_SERIA_NO).is(originalSeriaNo);
        criteria.and(QUERY_INVOICE_STATUS).is(Constants.INVOICE_STATUS_FINISHED);
        return getMongoTemplate().count(new Query(criteria), Trade.class);
    }

    @Override
    public String findSystemCodeBySeriaNo(String seriaNo) {
        BasicDBObject fieldsObject = new BasicDBObject();

        fieldsObject.put(COL_SYSTEM_CODE, true);

        DBObject queryObject = new BasicDBObject();
        queryObject.put(QUERY_INVOICE_SERIA_NO, seriaNo);

        Query query = new BasicQuery(queryObject, fieldsObject);

        Trade trade = this.getMongoTemplate().findOne(query, Trade.class);

        if(Objects.nonNull(trade)) {
            return trade.getSystemCode();
        }

        return null;
    }

    @Override
    public Trade findShopAndOrderBySeriaNo(String seriaNo) {
        BasicDBObject fieldsObject = new BasicDBObject();
        fieldsObject.put(COL_SHOP_CODE, true);
        fieldsObject.put(COL_ORDER_NO, true);

        DBObject queryObject = new BasicDBObject();
        queryObject.put(QUERY_INVOICE_SERIA_NO, seriaNo);

        Query query = new BasicQuery(queryObject, fieldsObject);

        return this.getMongoTemplate().findOne(query, Trade.class);
    }

    @Override
    public Trade findTradeByOrderNo(String orderNo) {
        return getMongoTemplate().findOne(new Query(Criteria.where(COL_ORDER_NO).is(orderNo)), Trade.class);
    }

    @Override
    public Pagination<InvoiceDO> queryInvoice(Page page, InvoiceQuery invoiceQuery) {
        List<AggregationOperation> aggregationOperationList = new ArrayList<>();
        aggregationOperationList.add(Aggregation.unwind(COL_INVOICE_LIST));
        aggregationOperationList.add(Aggregation.match(getCriteriaQueryInvoice(invoiceQuery)));

        return aggregatePage(page, aggregationOperationList, Trade.class, InvoiceDO.class);
    }

    @Override
    public List<InvoiceDO> findInvoiceByOrderNo(String orderNo) {
        List<AggregationOperation> aggregationOperationList = new ArrayList<>();
        aggregationOperationList.add(Aggregation.unwind(COL_INVOICE_LIST));
        aggregationOperationList.add(Aggregation.match(Criteria.where(COL_ORDER_NO).is(orderNo)));

        return  this.aggregateList(aggregationOperationList, Trade.class, InvoiceDO.class);
    }

    @Override
    public Pagination<InvoiceDO> queryInvoice(Page page, Sort[] sorts, InvoiceCommonQuery invoiceCommonQuery) {
        List<AggregationOperation> aggregationOperationList = new ArrayList<>();
        aggregationOperationList.add(Aggregation.unwind(COL_INVOICE_LIST));
        aggregationOperationList.add(Aggregation.match(getCommonQuery(invoiceCommonQuery)));

        aggregateSort(sorts, aggregationOperationList);

        return  this.aggregatePage(page, aggregationOperationList, Trade.class, InvoiceDO.class);
    }

    @Override
    public List<InvoiceDO> findInvoiceBySeriaNo(List<String> seriaNoList) {
        List<AggregationOperation> aggregationOperationList = new ArrayList<>();
        aggregationOperationList.add(Aggregation.unwind(COL_INVOICE_LIST));
        aggregationOperationList.add(Aggregation.match(Criteria.where(QUERY_INVOICE_SERIA_NO).in(seriaNoList)));

        return this.aggregateList(aggregationOperationList, Trade.class, InvoiceDO.class);
    }

    @Override
    public Boolean existsTrade(String orderNo, String seriaNo) {
        List<AggregationOperation> aggregationOperationList = new ArrayList<>();
        aggregationOperationList.add(Aggregation.unwind(COL_INVOICE_LIST));
        Criteria criteria = Criteria.where(COL_ORDER_NO).is(orderNo);
        criteria.and(QUERY_INVOICE_SERIA_NO).is(seriaNo);
        aggregationOperationList.add(Aggregation.match(criteria));

        int count = this.aggregateCount(aggregationOperationList, Trade.class);

        if(count>Constants.ZERO){
            return Boolean.TRUE;
        }else{
            return Boolean.FALSE;
        }
    }

    @Override
    public String findSeriaNo(Long oaRequestId) {
        List<AggregationOperation> aggregationOperationList = new ArrayList<>();
        aggregationOperationList.add(Aggregation.unwind(COL_INVOICE_LIST));
        Criteria criteria = Criteria.where(QUERY_OA_REQUEST_ID).is(oaRequestId);
        aggregationOperationList.add(Aggregation.match(criteria));
        InvoiceDO invoiceDO = this.aggregateOne(aggregationOperationList, Trade.class, InvoiceDO.class);
        if(Objects.nonNull(invoiceDO)&&Objects.nonNull(invoiceDO.getInvoiceList())){
            return invoiceDO.getInvoiceList().getSeriaNo();
        }else{
            return null;
        }
    }

    private Criteria getCommonQuery(InvoiceCommonQuery invoiceCommonQuery) {
        Criteria criteria = new Criteria();
        if(this.isNotEmpty(invoiceCommonQuery.getSeriaNo())){
            criteria.and(QUERY_INVOICE_SERIA_NO).is(invoiceCommonQuery.getSeriaNo());
        }
        if(this.isNotEmpty(invoiceCommonQuery.getOrderNo())){
            criteria.and(COL_ORDER_NO).is(invoiceCommonQuery.getOrderNo());
        }
        if(this.isNotEmpty(invoiceCommonQuery.getOrderDocNo())){
            criteria.and(COL_ORDER_DOC_NO).is(invoiceCommonQuery.getOrderDocNo());
        }
        if(this.isNotEmpty(invoiceCommonQuery.getInvoiceCarrier())){
            criteria.and(COL_INVOICE_CARRIER).is(invoiceCommonQuery.getInvoiceCarrier());
        }
        if(this.isNotEmpty(invoiceCommonQuery.getInvoiceType())){
            criteria.and(COL_INVOICE_TYPE).is(invoiceCommonQuery.getInvoiceType());
        }
        if(this.isNotEmpty(invoiceCommonQuery.getInvoiceTitleType())){
            criteria.and(QUERY_INVOICE_TITLE_TYPE).is(invoiceCommonQuery.getInvoiceTitleType());
        }
        if(this.isNotEmpty(invoiceCommonQuery.getInvoiceTitle())){
            criteria.and(QUERY_INVOICE_TITLE).is(invoiceCommonQuery.getInvoiceTitle());
        }
        if(this.isNotEmpty(invoiceCommonQuery.getRedOrBlue())){
            criteria.and(QUERY_RED_OR_BLUE).is(invoiceCommonQuery.getRedOrBlue());
        }
        if(this.isNotEmpty(invoiceCommonQuery.getIsReversed())){
            criteria.and(QUERY_IS_REVERSED).is(invoiceCommonQuery.getIsReversed());
        }
        if(this.isNotEmpty(invoiceCommonQuery.getTotalPrice())){
            Pattern pattern = Pattern.compile(String.format(Constants.PRICE_FORMAT, invoiceCommonQuery.getTotalPrice().toString()), Pattern.CASE_INSENSITIVE);
            criteria.and(QUERY_TOTAL_PRICE).regex(pattern);
        }
        if(this.isNotEmpty(invoiceCommonQuery.getTotalTax())){
            Pattern pattern = Pattern.compile(String.format(Constants.PRICE_FORMAT, invoiceCommonQuery.getTotalTax().toString()), Pattern.CASE_INSENSITIVE);
            criteria.and(QUERY_TOTAL_TAX).regex(pattern);
        }
        getDefaultQuery(invoiceCommonQuery, criteria);

        return criteria;
    }

    private void getDefaultQuery(InvoiceCommonQuery invoiceCommonQuery, Criteria criteria) {
        if(this.isNotEmpty(invoiceCommonQuery.getStatus())&&this.isNotEmpty(invoiceCommonQuery.getStatusList())){
            invoiceCommonQuery.getStatusList().add(invoiceCommonQuery.getStatus());
            criteria.and(QUERY_INVOICE_STATUS).in(invoiceCommonQuery.getStatusList());
        }else if(this.isNotEmpty(invoiceCommonQuery.getStatus())){
            criteria.and(QUERY_INVOICE_STATUS).is(invoiceCommonQuery.getStatus());
        }else if(this.isNotEmpty(invoiceCommonQuery.getStatusList())){
            criteria.and(QUERY_INVOICE_STATUS).in(invoiceCommonQuery.getStatusList());
        }
        if(this.isNotEmpty(invoiceCommonQuery.getApplyType())){
            criteria.and(QUERY_APPLY_TYPE).is(invoiceCommonQuery.getApplyType());
        }
        if(this.isNotEmpty(invoiceCommonQuery.getLastModifyLoginName())){
            criteria.and(QUERY_LAST_MODIFY_LOGIN_NAME).is(invoiceCommonQuery.getLastModifyLoginName());
        }
        if(this.isNotEmpty(invoiceCommonQuery.getLastModifyUserName())){
            criteria.and(QUERY_LAST_MODIFY_USER_NAME).is(invoiceCommonQuery.getLastModifyUserName());
        }
    }

    private Criteria getCriteriaQueryInvoice(InvoiceQuery invoiceQuery) {
        Criteria criteria = new Criteria();
        if(this.isNotEmpty(invoiceQuery.getShopCode())){
            criteria.and(COL_SHOP_CODE).is(invoiceQuery.getShopCode());
        }
        if(this.isNotEmpty(invoiceQuery.getOrderNo())){
            criteria.and(COL_ORDER_NO).is(invoiceQuery.getOrderNo());
        }
        if(this.isNotEmpty(invoiceQuery.getOrderDocNo())){
            criteria.and(COL_ORDER_DOC_NO).is(invoiceQuery.getOrderDocNo());
        }
        if(this.isNotEmpty(invoiceQuery.getSeriaNo())){
            criteria.and(QUERY_INVOICE_SERIA_NO).is(invoiceQuery.getSeriaNo());
        }
        if(this.isNotEmpty(invoiceQuery.getStatus())){
            criteria.and(QUERY_INVOICE_STATUS).is(invoiceQuery.getStatus());
        }
        if(this.isNotEmpty(invoiceQuery.getInvoiceType())){
            criteria.and(QUERY_INVOICE_TYPE).is(invoiceQuery.getInvoiceType());
        }
        if(this.isNotEmpty(invoiceQuery.getRedOrBlue())){
            criteria.and(QUERY_RED_OR_BLUE).is(invoiceQuery.getRedOrBlue());
        }
        if(this.isNotEmpty(invoiceQuery.getInvoiceTitleType())){
            criteria.and(QUERY_INVOICE_TITLE_TYPE).is(invoiceQuery.getInvoiceTitleType());
        }
        if(this.isNotEmpty(invoiceQuery.getInvoiceTitle())){
            criteria.and(QUERY_INVOICE_TITLE).is(invoiceQuery.getInvoiceTitle());
        }
        if(this.isNotEmpty(invoiceQuery.getIsReversed())){
            criteria.and(QUERY_IS_REVERSED).is(invoiceQuery.getIsReversed());
        }
        if(this.isNotEmpty(invoiceQuery.getTotal())){
            criteria.and(QUERY_TOTAL).is(invoiceQuery.getTotal());
        }
        if(this.isNotEmpty(invoiceQuery.getPayerMobile())){
            criteria.and(QUERY_PAYER_MOBILE).is(invoiceQuery.getPayerMobile());
        }
        if(this.isNotEmpty(invoiceQuery.getPayerEmail())){
            criteria.and(QUERY_PAYER_EMAIL).is(invoiceQuery.getPayerEmail());
        }
        return criteria;
    }

    private Update getUpdatePushInvoice(List<Invoice> invoiceList) {
        Update update = newUpdate();
        update.pushAll(COL_INVOICE_LIST, invoiceList.toArray());
        return update;
    }

    private Update newUpdate() {
        Update update = new Update();
        update.inc(COL_VERSION, 1);
        update.set(COL_LAST_MODIFY_TIME, new Date());
        return update;
    }

    private Update newUpdate(Trade trade) {
        Update update = new Update();
        update.inc(COL_VERSION, 1);
        update.set(COL_LAST_MODIFY_TIME, trade.getLastModifyTime());
        return update;
    }

}

三、写Junit UT
/**
 * 
 * @author Shuai Chen
 * 2019年1月10日   下午4:59:16
 *
 */
public class InvoiceDaoImplJunitTest extends BaseSpringBootTest{
    @Autowired
    private InvoiceDaoImpl invoiceDaoImpl;
    
    private Trade trade = TradeFactory.newInstance6();
    private Trade trade2 = TradeFactory.newInstance7();
    private Trade trade3 = TradeFactory.newInstance8();
    
    @Before
    public void init() {
        mongoTemplate.insert(trade);
        mongoTemplate.insert(trade2);
        mongoTemplate.insert(trade3);
    }
    
    @Test
    public void 根据平台订单号看订单是否存在dao_成功() {
        //given
        String orderNo = trade.getOrderNo();
        
        //when
        Boolean actual = invoiceDaoImpl.existsTrade(orderNo);
        
        //then
        assertTrue(actual);
    }
    
    @Test
    public void 根据平台订单号和订单航id看订单是否存在dao_成功() {
        //given
        String orderNo = trade.getOrderNo();
        String itemId = trade.getInvoiceList().iterator().next().getInvoiceLineList().iterator().next().getItemId();
        
        //when
        Boolean actual = invoiceDaoImpl.existsItemId(orderNo, itemId);
        
        //then
        assertTrue(actual);
    }
    
    @Test
    public void 查找未驳回蓝票dao_成功() {
        //given
        RedBlueInvoiceCreateDTO redInvoiceCreateTrade = new RedBlueInvoiceCreateDTO();
        redInvoiceCreateTrade.setShopCode(trade.getShopCode());
        redInvoiceCreateTrade.setPfDocNo(trade.getOrderNo());
        
        //when
        List<InvoiceDO> invoiceList = invoiceDaoImpl.findNotReversedBlueInvoice(redInvoiceCreateTrade);
        
        //then
        assertTrue(CollectionUtils.isNotEmpty(invoiceList));
    }
    
    @Test
    public void 查找需要驳回蓝票dao_成功() {
        //given
        RedBlueInvoiceCreateDTO redInvoiceCreateTrade = new RedBlueInvoiceCreateDTO();
        redInvoiceCreateTrade.setShopCode(trade.getShopCode());
        redInvoiceCreateTrade.setPfDocNo(trade.getOrderNo());
        
        String seriaNo = trade.getInvoiceList().iterator().next().getSeriaNo();
        Set<String> reversedSeriaNoSet = new HashSet<>(Arrays.asList(seriaNo));
        
        //when
        List<InvoiceDO> invoiceList = invoiceDaoImpl.findNeedReversedBlueInvoice(redInvoiceCreateTrade, reversedSeriaNoSet);
        
        //then
        assertTrue(CollectionUtils.isNotEmpty(invoiceList));
    }
    
    @Test
    public void 查找未发布电子发票dao_成功() {
        //given
        String shopCode = trade.getShopCode();
        Integer size = 9;

        //when
        List<InvoiceDO> invoiceList = invoiceDaoImpl.findNotIssuedElectronicInvoiceList(shopCode, size);
        
        //then
        assertTrue(CollectionUtils.isNotEmpty(invoiceList));
    }
    
    @Test
    public void 根据店铺编码A平台定单号A发票状态查找发票信息dao_成功(){
        //given
        String shopCode = trade.getShopCode();
        String orderNo = trade.getOrderNo();
        Integer status = Constants.INVOICE_STATUS_CANCEL;
        
        //when
        List<InvoiceDO> invoiceList = invoiceDaoImpl.findInvoiceByOrderNoStatus(shopCode, orderNo, status);
        
        //then
        assertTrue(CollectionUtils.isNotEmpty(invoiceList));
    }
    
    @Test
    public void 根据店铺编码和平台订单号查找未完成红票dao_成功(){
        //given
        String shopCode = trade2.getShopCode();
        String orderNo = trade2.getOrderNo();
        
        //when
        Integer actual = invoiceDaoImpl.findNotFinishedRedInvoiceCount(shopCode, orderNo);
        
        //then
        assertTrue(actual >= 1);
    }
    
    @Test
    public void 根据店铺编码和原发票代码查找完成的蓝票dao_成功() {
        //given
        String shopCode = trade3.getShopCode();
        String originalSeriaNo = trade3.getInvoiceList().iterator().next().getOriginalSeriaNo();
        
        //when
        Long actual = invoiceDaoImpl.findFinishedBlueInvoice(shopCode, originalSeriaNo);
        
        //then
        assertTrue(actual >= 1);
    }
    
    @Test
    public void 根据发票流水号查询系统编码dao_成功() {
        //given
        String seriaNo = trade.getInvoiceList().iterator().next().getSeriaNo();
        String expectedSystemCode = trade.getSystemCode();
        
        //when
        String actualSystemCode = invoiceDaoImpl.findSystemCodeBySeriaNo(seriaNo);
        
        //then
        assertEquals(expectedSystemCode, actualSystemCode);
    }
    
    @Test
    public void 根据发票流水号及其他合法参数查询订单dao_成功() {
        //given
        String seriaNo = trade.getInvoiceList().iterator().next().getSeriaNo();
        
        //when
        Trade actualTrade = invoiceDaoImpl.findShopAndOrderBySeriaNo(seriaNo);
        
        //then
        assertEquals(trade.getId(), actualTrade.getId());
    }
    
    @Test
    public void 根据发票流水号查询订单dao_成功() {
        //given
        String orderNo = trade.getOrderNo();
        
        //when
        Trade actualTrade = invoiceDaoImpl.findTradeByOrderNo(orderNo);
        
        //then
        assertEquals(trade.getId(), actualTrade.getId());
    }
    
    @Test
    public void 分页查询发票dao_成功() {
        //given
        Page page = new Page();
        InvoiceQuery invoiceQuery = InvoiceQueryFactory.newInstance1();
        
        //when
        Pagination<InvoiceDO> pageQuery = invoiceDaoImpl.queryInvoice(page, invoiceQuery);
        
        //then
        assertTrue(pageQuery.getItems().size() > 0);
    }
    
    @Test
    public void 根据发票流水号查询发票dao_成功() {
        //given
        String orderNo = trade.getOrderNo();
        
        //when
        List<InvoiceDO> invoiceList = invoiceDaoImpl.findInvoiceByOrderNo(orderNo);
        
        //then
        assertTrue(CollectionUtils.isNotEmpty(invoiceList));
    }
    
    //274行
    
    @After
    public void destory() {
        WriteResult wr = mongoTemplate.remove(Query.query(Criteria.where("_id").is(trade.getId())), Trade.class);
        assertTrue(1 == wr.getN());
        WriteResult wr2 = mongoTemplate.remove(Query.query(Criteria.where("_id").is(trade2.getId())), Trade.class);
        assertTrue(1 == wr2.getN());
        WriteResult wr3 = mongoTemplate.remove(Query.query(Criteria.where("_id").is(trade3.getId())), Trade.class);
        assertTrue(1 == wr3.getN());
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值