Junit如何进行多线程测试

来源:互联网 发布:美工岗位职责 编辑:程序博客网 时间:2024/05/22 08:00

刚学Java多线程时遇到的一个问题:

package chapter_one;/** * 继承 Thread 类 实现多线程 */public class MyThread extends Thread {    public MyThread() {        System.out.println("MyThread::" + Thread.currentThread().getName());    }    @Override    public void run() {        System.out.println("run::" + Thread.currentThread().getName());    }}

在Junit中测试:

@Test    public void testCurrentThread() {        Thread thread = new MyThread();        thread.start();    }

输出:
MyThread::main
Where is run::Thread-0?
如此简单的一个测试程序居然都有问题?

在普通的类中测试则没有问题:

package chapter_one;public class Main {    public static void main(String[] args) {        Thread thread = new MyThread();        thread.start();    }}

那么就有理由推断是Junit的问题了。

Junit本身是不支持普通的多线程测试的,这是因为Junit的底层实现上,是用System.exit退出用例执行的。JVM都终止了,在测试线程启动的其他线程自然也无法执行。

org\junit\runner\JUnitCore.java

    public static void main(String... args) {        Result result = new JUnitCore().runMain(new RealSystem(), args);        System.exit(result.wasSuccessful() ? 0 : 1);    }

所以要想编写多线程Junit测试用例,就必须让主线程等待所有子线程执行完成后再退出,使用Thread.join()方法。

@Test    public void testCurrentThread() {        //返回代码正在被哪个线程调用的信息//        System.out.println(Thread.currentThread());        Thread thread = new MyThread();        thread.start();        try {            thread.join();        } catch (InterruptedException e) {            e.printStackTrace();        }    }

输出:
MyThread::main
run::Thread-0

原创粉丝点击