扩展Junit为多线程。
Junit源代码会执行System.exit退出,主线程终止jvm都停了,其他线程肯定执行不了的。
使用GroboUtils可以去下载它的jar包,官网链接点击打开链接。
或者使用maven依赖:
- <dependency>
- <groupId>net.sourceforge.groboutils</groupId>
- <artifactId>groboutils-core</artifactId>
- <version>5</version>
- </dependency>
代码很简单:
- @Test
- public void testThreadJunit() throws Throwable {
-
- TestRunnable[] trs = new TestRunnable [10];
- for(int i=0;i<10;i++){
- trs[i]=new ThreadA();
- }
-
-
- MultiThreadedTestRunner mttr = new MultiThreadedTestRunner(trs);
-
-
- mttr.runTestRunnables();
-
-
- }
- // 自定义的线程
- private class ThreadA extends TestRunnable {
- @Override
- public void runTest() throws Throwable {
-
- myCommMethod2();
- }
- }
- // 测试方法
- public void myCommMethod2() throws Exception {
- System.out.println("===" + Thread.currentThread().getId() + "begin to execute myCommMethod2");
- }