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();

    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM