2、建立监控的线程,每个监控对应一个线程,也可以一个监控监控整个测试过程,需要传入监控的线程对象。
/** * */ package com.dtsz.groboTest; import net.sourceforge.groboutils.junit.v1.TestMonitorRunnable; import net.sourceforge.groboutils.junit.v1.TestRunnable; /** * @author xiaoli * */ public class TestMonitorRunnable1 extends TestMonitorRunnable { private int i; private TestRunnable t; public TestMonitorRunnable1(int i,TestRunnable t) { super(); this.i = i; this.t = t; } /* (non-Javadoc) * @see net.sourceforge.groboutils.junit.v1.TestMonitorRunnable#runMonitor() */ @Override public void runMonitor() throws Throwable { System.out.println(i+"线程监控正在跑…………状态:"+t.isDone()); } } |
3、建立主测试类进行并发单元测试,这里只有简单数据打印,具体情况传入数据进行测试,比如Web项目中需要在setUp()中部署好相关的环境等:
/** * */ package com.dtsz.groboTest; import junit.framework.TestCase; import net.sourceforge.groboutils.junit.v1.MultiThreadedTestRunner; import net.sourceforge.groboutils.junit.v1.TestMonitorRunnable; import net.sourceforge.groboutils.junit.v1.TestRunnable; import org.junit.Test; /** * @author xiaoli * */ public class MainTest1 extends TestCase{ @Override protected void setUp() throws Exception { // TODO Auto-generated method stub super.setUp(); System.out.println("setUp()数据准备"); } @Override protected void tearDown() throws Exception { // TODO Auto-generated method stub super.tearDown(); System.out.println("tearDown()结束"); } @Test public void test1() throws Throwable { int count = 2; long time = 0; TestRunnable[] tr = new TestRunnable[count]; TestMonitorRunnable [] trm = new TestMonitorRunnable[count]; for(int i = 0;i<count;i++) { TestRunnable1 t = new TestRunnable1(i,(i+1)*time); TestMonitorRunnable1 m = new TestMonitorRunnable1(i,t); tr[i] = t; trm[i] = m; } MultiThreadedTestRunner mttr = new MultiThreadedTestRunner(tr,trm); //没有在该时间内完成的线程将会被杀掉 mttr.runTestRunnables(); } } |