zoukankan      html  css  js  c++  java
  • spring test + junit 测试中使用 线程 , 代码未执行完就结束了

    spring test + junit  测试中使用 线程 , 代码未执行完就结束了 ,可能是junit 强制主线程结束,导致子线程也被强制结束了。

    可以使用CountDownLatch 等 方法 使主线程等待 。 

    测试用例如下: 

        @Test
        public void getUpmerchantTest() throws InterruptedException {
            //MerchantInfo merchantInfo, ShowRequestTransaction reqBody,TransLogInfo transLogInfo,ShowResponseTransaction
            //showResponse;
            int count = 100 ;
            CyclicBarrier barrier = new CyclicBarrier(count); //线程栅栏,等待所有线程都准备好,同时执行
            CountDownLatch latch = new CountDownLatch(1);
            for (int i=0;i<count;i++){
                Thread t = new Thread(() -> {
                    try {
                        barrier.await();
                        ShowRequestTransaction reqBody = new ShowRequestTransaction();
                        reqBody.setIsFirst("0");
                        reqBody.setPickMerId("872290045110204");
                        reqBody.setPickTrmNo("08000181");
                        TransLogInfo transLogInfo = new TransLogInfo();
                        transLogInfo.setTransAmount(new BigDecimal("0.01"));
                        UpMerchantInfo info = payHandler.getUpMerchantInfo(new MerchantInfo(),reqBody,transLogInfo,new ShowResponseTransaction());
                        System.out.println("新的流水号: " +(info==null?null:info.getTransNo()));
                   } catch (Exception e) {
                        e.printStackTrace();
                    }
                });
                t.start();
            }
            latch.await();
    
        }
  • 相关阅读:
    SQL LOADER使用
    固定资产新增接口
    固定资产的调整分配接口
    固定资产的完全报废接口
    固定资产更新接口
    详解EBS接口开发之库存事务处理采购接收和退货
    物料分类新增&更新
    物料REVISION控制
    供应商导入的API补充(详解EBS接口开发之供应商导入)
    PostgreSQL经常使用函数
  • 原文地址:https://www.cnblogs.com/zhangchenglzhao/p/10914560.html
Copyright © 2011-2022 走看看