Using Groboutils for simple concurrency unit testing, which enables testing and monitoring and control of individual thread execution, shows the test process for simple cases:
1, establish the thread TestRunnable1 to test
/**
* * * * * *
package com.dtsz.groboTest;
Import net.sourceforge.groboutils.junit.v1.TestRunnable;
/**
* @author Xiaoli
*
*
/public class TestRunnable1 extends testrunnable {
private int i;
Private long sleeptime;
Public TestRunnable1 (int i, long sleeptime) {
super ();
this.i = i;
This.sleeptime = Sleeptime;
}
/* (Non-javadoc)
* @see net.sourceforge.groboutils.junit.v1.testrunnable#runtest () * * *
@Override Public
void Runtest () throws Throwable {
//TODO auto-generated method Stub
System.out.println (i+ "thread running ... ... ");
This.delay (sleeptime);
System.out.println (i+ "line one thread to be finished ...");
}
}.
2, the establishment of monitoring of the thread, each monitoring corresponding to a thread, can also monitor the entire testing process, the need for incoming monitoring of the thread object.
/**
* * * * * *
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+ "thread monitoring is running ...) Status: "+t.isdone ());
}
}
3, the establishment of the main test class for concurrent unit testing, where only simple data printing, the specific situation of incoming data for testing, such as the Web project needs in the setup () to deploy a good related environment, etc.:
/** * * * * * * 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
{//TODO auto-generated Method Stub super.setup ();
System.out.println ("setUp () data Preparation");
@Override protected void teardown () throws Exception {//TODO auto-generated Method Stub Super.teardown ();
System.out.println ("teardown () end");
@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);
A thread that is not completed within that time will be killed Mttr.runtestrunnables ();
}
}
Test results: The monitor runs in real time every few milliseconds, knowing that the entire unit test is complete:
SETUP () Data preparation
1 thread monitoring is running ... ..... Status: False
0 thread monitoring is running ... ..... Status: False
0 The thread is running .....
1 The thread is running .....
1 thread monitoring is running ... ..... Status: False
0 thread monitoring is running ... ..... Status: False
0 Line one thread going to finish ......
1 line one thread going to finish ......
1 thread monitoring is running ... ..... Status: False
0 thread monitoring is running ... ..... Status: False
1 thread monitoring is running ... ..... Status: False
0 thread monitoring is running ... ..... Status: False
1 thread monitoring is running ... ..... Status: False
0 thread monitoring is running ... ..... Status: False
1 thread monitoring is running ... ..... Status: False
0 thread monitoring is running ... ..... Status: True
1 thread monitoring is running ... ..... Status: True
Teardown () End
The specific function can be extended by this.