Spring Boot JUnit 测试类执行Cotroller操作

1.@SpringBootTest注解是SpringBoot自1.4.0版本开始引入的一个用于测试的注解 

2.@RunWith 翻译成中文就是 测试运行器,JUnit所有的测试方法都是由测试运行器负责执行。

3.@EnableAutoConfiguration自动配置

三者缺一不可

这样就不用来回的启动项目了,减少时间,提高开发效率

@SpringBootTest(classes= JobReportRun.class)
@RunWith(SpringRunner.class)
@EnableAutoConfiguration
public class UpdateDemo {

    @Autowired
    private BasicOrdersMapper basicOrdersMapper;


    @Autowired
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    private static Logger logger= LoggerFactory.getLogger(WaimaiOrdersReportService.class);

    @Test
    public void test1(){
        String createDate= DateUtils.convert2YestadayString(-1,"yyyy-MM-dd");
        //查询外卖订单数据
        int orderCount=0,orderItemsCount=0;
        try{

            List<ReportOrders> reportOrdersList=basicOrdersMapper.selectReportAllOrdersList(createDate);

            //合并所有的订单明细
            List<ReportOrderItems> reportOrderItemsList=new ArrayList<>();
            for(int j=0;j<reportOrdersList.size();j++){
                reportOrderItemsList.addAll(reportOrdersList.get(j).getReportItemsList());
            }

            if(reportOrdersList.size()>0){

                //插入订单数据
                orderCount=insertOrders(namedParameterJdbcTemplate,reportOrdersList,"etoneO2o");

                //插入订单明细数据
                orderItemsCount=insertOrderItems(namedParameterJdbcTemplate,reportOrderItemsList,"etoneO2o");
                //1.查询除平台以外的所有商圈中外卖授权的商户
                List<String> waimaiTradingMerchantBn = basicOrdersMapper.selectWaiMaiTradingMerchantBn();
                int count = 0;
                //根据这些商户号查询对应的易客来商户号和对应的商圈
                if(waimaiTradingMerchantBn.size()>0){
                    List<Map<Object,Object>> waimaiMT = basicOrdersMapper.selectWaiMaiMerchantBnAndDb(waimaiTradingMerchantBn);
                    //如果不为空就直接更新之前错误的数据所有的外卖source_name=etoneO2o
                    if(waimaiMT.size()>0){
                        for (Map<Object, Object> map : waimaiMT) {
                            count = basicOrdersMapper.updateWaiMaiSourceName(map);

                        }
                        logger.info("-------------总计更新数据:【"+count+"】条数据--------------");
                    }
                }

            }
        }catch (CannotGetJdbcConnectionException e){
            

        }catch (Exception e) {
         
        }
      
        );
    }

    public int insertOrders(NamedParameterJdbcTemplate jdbcTemplateManal,List<ReportOrders> ordersList,String sourceName) {
        int[] aa={};
        if(ordersList.size()>0) {
            Object[] obj = new Object[ordersList.size()];
            for (int i = 0; i < ordersList.size(); i++) {
                obj[i] = ordersList.get(i);
            }
            //批量提交
            SqlParameterSource[] params = SqlParameterSourceUtils.createBatch(obj);
            String insertSql = "insert into sdb_b2c_orders(order_id,seller_order_id,total_amount,final_amount,pay_status,ship_status,createtime,merchant_bn,source,source_name)" +
                    "VALUES(:orderId,:sellerOrderId,:totalAmount,:finalAmount,:payStatus,:shipStatus,:createTime,:merchantBn,:source,'"+sourceName+"')";
            aa= jdbcTemplateManal.batchUpdate(insertSql, params);
        }
        return aa.length;
    }

    public int insertOrderItems(NamedParameterJdbcTemplate jdbcTemplateManal,List<ReportOrderItems> itemsList,String sourceName) {
        int[] bb={};
        if(itemsList.size()>0) {
            Object[] obj = new Object[itemsList.size()];
            for (int i = 0; i < itemsList.size(); i++) {
                obj[i] = itemsList.get(i);
            }
            //批量提交
            SqlParameterSource[] params = SqlParameterSourceUtils.createBatch(obj);
            String insertSql = "insert into sdb_b2c_order_items(order_id,`name`,price,amount,nums,merchant_bn,source_name)" +
                    "VALUES(:orderId,:name,:price,:amount,:nums,:merchantBn,'"+sourceName+"')";
            bb= jdbcTemplateManal.batchUpdate(insertSql, params);
        }
        return bb.length;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值