TestNG非同期タスクのテストのアイデア

2846 ワード

public class TestResultBean {
    private String name;
    private boolean result;

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name
     *            the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * @return the result
     */
    public boolean isResult() {
        return result;
    }

    /**
     * @param result
     *            the result to set
     */
    public void setResult(boolean result) {
        this.result = result;
    }

}

static List rList = new ArrayList<>();
List tList = new ArrayList<>();

public void test01() {
    TestResultBean test01 = new TestResultBean();
    test01.setName("test01");
    Thread a = new Thread(new Runnable() {
        @Override
        public void run() {
            System.out.println(" ");
            try {
                Thread.sleep(2000);
                test01.setResult(true);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    });
    rList.add(test01);
    tList.add(a);
}

public void test02() {
    System.out.println("Case02");
    TestResultBean test02 = new TestResultBean();
    test02.setName("test02");
    try {
        Thread b = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println(" b");
                try {
                    Thread.sleep(3000);
                    test02.setResult(false);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        rList.add(test02);
        tList.add(b);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public void test03() {
    System.out.println("Case03");
    TestResultBean test03 = new TestResultBean();
    test03.setName("test03");
    Thread c = new Thread(new Runnable() {
        @Override
        public void run() {
            System.out.println(" b");
            test03.setResult(true);
        }
    });
    rList.add(test03);
    tList.add(c);
}

@Test
public void test04() {
    System.out.println("Case04");
    try {
        test01();
        test02();
        test03();
        ThreadUtil.execute(tList, tList.size(), 6000, true);

        for (TestResultBean r : rList) {
            System.out.println(r.getName() + "----" + r.isResult());
        }
        long result = rList.stream().filter(s -> s.isResult() == false).count();
        Assert.assertEquals(result == 0, true, " " + result);
    } catch (Exception e) {

    }
}

やはり気分が悪いですが、少し時間を節約しました.