JUNIT4 GroboUtils多线程测试
利用JUNIT4,GroboUtils进行多线程测试
多线程编程和测试一直是比较难搞的事情,特别是多线程测试。只用充分的测试,才可以发现多线程编码的潜在BUG。下面就介绍一下我自己在测试多线程并发程序时用的一个比较简单好用的测试工具类库。即JUNIT4和GroboUtils。
废话不多说,把代码贴出来,大家一看就明白了。
- import java.util.ArrayList;
- import java.util.HashSet;
- import java.util.Hashtable;
- import java.util.List;
- import java.util.Map;
- import java.util.Set;
- import net.sourceforge.groboutils.junit.v1.MultiThreadedTestRunner;
- import net.sourceforge.groboutils.junit.v1.TestRunnable;
- import org.junit.After;
- import org.junit.Before;
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class MutiThreadTest {
- //此处可以声明一些公共变量
- static ApplicationContext context = null;
- static String[] path = new String[] { "" };
- static Map<String, String> countMap = new Hashtable<String, String>();
- static Map<String, String> countMap2 = new Hashtable<String, String>();
- static Set<String> countSet = new HashSet<String>();
- static List<String> list = new ArrayList<String>();
- @Before
- public void setUp() throws Exception {
- context = new ClassPathXmlApplicationContext(path);
- }
- @After
- public void tearDown() throws Exception {
- context = null;
- }
- /**
- * JUNIT会运行这个方法,是主线程
- */
- @Test
- public void testThreadJunit() throws Throwable {
- //TestRunnable,实例化自定义的7个线程
- TestRunnable tr1, tr2, tr3, tr4, tr5, tr6, tr7;
- tr1 = new ThreadA();
- tr2 = new ThreadB();
- tr3 = new ThreadC();
- tr4 = new ThreadD();
- tr5 = new ThreadE();
- tr6 = new ThreadF();
- tr7 = new ThreadG();
- //必须声明为一个数组,把该数组当参数传递给 MultiThreadedTestRunner
- TestRunnable[] trs = { tr1, tr2, tr3, tr4, tr5, tr6, tr7 };
- //不需改动
- MultiThreadedTestRunner mttr = new MultiThreadedTestRunner(trs);
- //执行MTTR和7线程
- mttr.runTestRunnables();
- }
- /**
- * 要运行多线程,首先要实现自定义的线程</br>
- * 如下我定义了A,B,C,D,E,F,G七个线程</br>
- * 注意:自定义线程必须要继承TestRunnable</br>
- * 并且覆盖runTest()方法
- *
- */
- private class ThreadA extends TestRunnable {
- @Override
- public void runTest() throws Throwable {
- //线程要调用的方法或者要执行的操作
- myCommMethod2();
- }
- }
- private class ThreadB extends TestRunnable {
- @Override
- public void runTest() throws Throwable {
- myCommMethod2();
- }
- }
- private class ThreadC extends TestRunnable {
- @Override
- public void runTest() throws Throwable {
- myCommMethod2();
- }
- }
- private class ThreadD extends TestRunnable {
- @Override
- public void runTest() throws Throwable {
- myCommMethod2();
- }
- }
- private class ThreadE extends TestRunnable {
- @Override
- public void runTest() throws Throwable {
- myCommMethod2();
- }
- }
- private class ThreadF extends TestRunnable {
- @Override
- public void runTest() throws Throwable {
- myCommMethod2();
- }
- }
- private class ThreadG extends TestRunnable {
- @Override
- public void runTest() throws Throwable {
- myCommMethod2();
- }
- }
- /**
- * 线程要调用的方法。在此方法中</br>
- * 实现你的多线程代码测试。
- * @throws Exception
- */
- public void myCommMethod2() throws Exception {
- System.out.println("线程===" + Thread.currentThread().getId() + "执行myCommMethod2操作开始");
- for (int i = 0; i <10; i++) {
- int a = i*5;
- System.out.println(a);
- }
- System.out.println("线程===" + Thread.currentThread().getId() + "执行myCommMethod2操作结束");
- }
- }
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Set; import net.sourceforge.groboutils.junit.v1.MultiThreadedTestRunner;
import net.sourceforge.groboutils.junit.v1.TestRunnable; import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class MutiThreadTest {
//此处可以声明一些公共变量
static ApplicationContext context = null;
static String[] path = new String[] { "" };
static Map<String, String> countMap = new Hashtable<String, String>();
static Map<String, String> countMap2 = new Hashtable<String, String>();
static Set<String> countSet = new HashSet<String>();
static List<String> list = new ArrayList<String>();@Before
public void setUp() throws Exception {
context = new ClassPathXmlApplicationContext(path);
} @After
public void tearDown() throws Exception {
context = null;
}
/**
* JUNIT会运行这个方法,是主线程
*/
@Test
public void testThreadJunit() throws Throwable {
//TestRunnable,实例化自定义的7个线程
TestRunnable tr1, tr2, tr3, tr4, tr5, tr6, tr7;
tr1 = new ThreadA();
tr2 = new ThreadB();
tr3 = new ThreadC();
tr4 = new ThreadD();
tr5 = new ThreadE();
tr6 = new ThreadF();
tr7 = new ThreadG();
//必须声明为一个数组,把该数组当参数传递给 MultiThreadedTestRunner
TestRunnable[] trs = { tr1, tr2, tr3, tr4, tr5, tr6, tr7 };
//不需改动
MultiThreadedTestRunner mttr = new MultiThreadedTestRunner(trs);
//执行MTTR和7线程
mttr.runTestRunnables();
} /**
* 要运行多线程,首先要实现自定义的线程</br>
* 如下我定义了A,B,C,D,E,F,G七个线程</br>
* 注意:自定义线程必须要继承TestRunnable</br>
* 并且覆盖runTest()方法
*
*/
private class ThreadA extends TestRunnable {
@Override
public void runTest() throws Throwable {
//线程要调用的方法或者要执行的操作
myCommMethod2();
}
} private class ThreadB extends TestRunnable {
@Override
public void runTest() throws Throwable {
myCommMethod2();
}
} private class ThreadC extends TestRunnable {
@Override
public void runTest() throws Throwable {
myCommMethod2();
}
} private class ThreadD extends TestRunnable {
@Override
public void runTest() throws Throwable {
myCommMethod2();
}
} private class ThreadE extends TestRunnable {
@Override
public void runTest() throws Throwable {
myCommMethod2();
}
} private class ThreadF extends TestRunnable {
@Override
public void runTest() throws Throwable {
myCommMethod2();
}
} private class ThreadG extends TestRunnable {
@Override
public void runTest() throws Throwable {
myCommMethod2();
}
} /**
* 线程要调用的方法。在此方法中</br>
* 实现你的多线程代码测试。
* @throws Exception
*/
public void myCommMethod2() throws Exception {
System.out.println("线程===" + Thread.currentThread().getId() + "执行myCommMethod2操作开始");
for (int i = 0; i <10; i++) {
int a = i*5;
System.out.println(a);
}
System.out.println("线程===" + Thread.currentThread().getId() + "执行myCommMethod2操作结束");
}
}
参考文章:
[url]
http://www.ibm.com/developerworks/cn/java/j-lo-test-multithread/index.html?ca=drs-
[/url]
[url]
http://groboutils.sourceforge.net/index.html[/url]
<ul>
<li><a href="http://dl.iteye.com/topics/download/9a171d91-b8f2-34cd-934e-5207ccdb61ab">JUNIT多线程测试.rar</a> (141.5 KB)</li>
<li>下载次数: 104</li>
</ul>
JUNIT4 GroboUtils多线程测试的更多相关文章
- Junit使用GroboUtils进行多线程测试
写过Junit单元测试的同学应该会有感觉,Junit本身是不支持普通的多线程测试的,这是因为Junit的底层实现上,是用System.exit退出用例执行的.JVM都终止了,在测试线程启动的其他线程自 ...
- 关于JUnit4无法支持多线程测试的解决方法
转自:https://segmentfault.com/a/1190000003762719 其实junit是将test作为参数传递给了TestRunner的main函数.并通过main函数进行执行. ...
- 使用Spring+Junit4.4进行测试(使用注解)
http://nottiansyf.iteye.com/blog/345819 使用Junit4.4测试 在类上的配置Annotation @RunWith(SpringJUnit4ClassRunn ...
- 用Spring+Junit4.4进行测试(使用注解)
http://nottiansyf.iteye.com/blog/345819 使用Junit4.4测试 在类上的配置Annotation @RunWith(SpringJUnit4ClassRunn ...
- 使用Spring+Junit4.4进行测试
http://nottiansyf.iteye.com/blog/345819 使用Junit4.4测试 在类上的配置Annotation @RunWith(SpringJUnit4ClassRunn ...
- testng入门教程12 TestNG执行多线程测试
testng入门教程 TestNG执行多线程测试 testng入门教程 TestNG执行多线程测试 并行(多线程)技术在软件术语里被定义为软件.操作系统或者程序可以并行地执行另外一段程序中多个部分或者 ...
- TestNG多线程测试-注解方式实现
用@Test(invocationCount = x,threadPoolSize = y)声明,invocationCount表示执行次数,threadPoolSize表示线程池大小. packag ...
- TestNg之XMl形式实现多线程测试
为什么要使用多线程测试? 在实际测试中,为了节省测试时间,提高测试效率,在实际测试场景中经常会采用多线程的方式去执行,比如爬虫爬数据,多浏览器并行测试. 关于多线程并行测试 TestNG中实现多线程并 ...
- TestNG 多线程测试
TestNG以注解的方式实现多线程测试 import org.testng.annotations.Test; public class TreadDemo { // invocationCount ...
随机推荐
- java实现视频断点上传文件
一.概述 所谓断点续传,其实只是指下载,也就是要从文件已经下载的地方开始继续下载.在以前版本的HTTP协议是不支持断点的,HTTP/1.1开始就支持了.一般断点下载时才用到Range和Content- ...
- Cisco基础(三):HSRP配置、三层交换配置HSRP、STP的配置、三层交换配置STP
一.HSRP配置 目标: 在企业网络到外部的连接方案中,要求不高的条件下可以是单出口.一旦该出口线路出现问题,整个企业网络就不能连接到外网了.为了使得企业网络到外网连接的高可用性,可以设置两个以上的出 ...
- SPFA算法的SLF优化 ——loj#10081. 「一本通 3.2 练习 7」道路和航线
今天做到一道最短路的题,原题https://loj.ac/problem/10081 题目大意为给一张有n个顶点的图,点与点之间有m1条道路,m2条航线,道路是双向的,且权值非负,而航线是单向的,权值 ...
- vim编辑器快捷键
光标控制命令 命令 光标移动 h或^h 向左移一个字符 j或^j或^n 向下移一行 k或^p 向上移一行 l或空格 向右移一个字符 G 移到文件的最后一行 nG 移到文件的第n行 w 移到下一个字的开 ...
- PCA 最大方差理论的直观解释
PCA 这个名字看起来比较玄乎,其实就是给数据换一个坐标系,然后非常生硬地去掉一些方差很小的坐标轴. 例:三维空间中,有一些数据只分布在一个平面上,我们通过"坐标系旋转变换",使得 ...
- [LeetCode]-algorithms-Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- css中如何使用border属性与display属性
border属性介绍 border属性设置元素边框. 边框3个要素如:粗细.线型.颜色. 边框线型属性值说明表如: 属性指 描述 none 定义无边框. hidden 与 "none&quo ...
- Gridview中显示的值根据数据库中带出的值作更改
前台页面对Gridview增加事件 OnRowDataBound="GridView1_RowDataBound"protected void GridView1_RowDataB ...
- P1364 医院设置 (补锅,memset初始化较大值不可用0x7fffffff )
P1364 医院设置 题解 弗洛伊德水过 注意初始化一个大数 0x3f 可以,0x5f 好像也可以,但是0x7fffffff 我是真的炸了,初始化为-1 (后面补锅有详细解释) 代码 #include ...
- 如何设置linux bash终端的字符显示内容和颜色?
通常linux有1-6个字符终端 tty, 有1个图形终端. 通常用 ctrl+alt+f1 到f6是字符终端, ctrl+alt+f7为图形终端, 但是, 也有不一样的, 如: fedora的4.0 ...