利用JUNIT4,GroboUtils进行多线程测试



多线程编程和测试一直是比较难搞的事情,特别是多线程测试。只用充分的测试,才可以发现多线程编码的潜在BUG。下面就介绍一下我自己在测试多线程并发程序时用的一个比较简单好用的测试工具类库。即JUNIT4和GroboUtils。

废话不多说,把代码贴出来,大家一看就明白了。

  1. import java.util.ArrayList;
  2. import java.util.HashSet;
  3. import java.util.Hashtable;
  4. import java.util.List;
  5. import java.util.Map;
  6. import java.util.Set;
  7. import net.sourceforge.groboutils.junit.v1.MultiThreadedTestRunner;
  8. import net.sourceforge.groboutils.junit.v1.TestRunnable;
  9. import org.junit.After;
  10. import org.junit.Before;
  11. import org.junit.Test;
  12. import org.springframework.context.ApplicationContext;
  13. import org.springframework.context.support.ClassPathXmlApplicationContext;
  14. public class MutiThreadTest {
  15. //此处可以声明一些公共变量
  16. static ApplicationContext context = null;
  17. static String[] path = new String[] { "" };
  18. static Map<String, String> countMap = new Hashtable<String, String>();
  19. static Map<String, String> countMap2 = new Hashtable<String, String>();
  20. static Set<String> countSet = new HashSet<String>();
  21. static List<String> list = new ArrayList<String>();
  22. @Before
  23. public void setUp() throws Exception {
  24. context = new ClassPathXmlApplicationContext(path);
  25. }
  26. @After
  27. public void tearDown() throws Exception {
  28. context = null;
  29. }
  30. /**
  31. * JUNIT会运行这个方法,是主线程
  32. */
  33. @Test
  34. public void testThreadJunit() throws Throwable {
  35. //TestRunnable,实例化自定义的7个线程
  36. TestRunnable tr1, tr2, tr3, tr4, tr5, tr6, tr7;
  37. tr1 = new ThreadA();
  38. tr2 = new ThreadB();
  39. tr3 = new ThreadC();
  40. tr4 = new ThreadD();
  41. tr5 = new ThreadE();
  42. tr6 = new ThreadF();
  43. tr7 = new ThreadG();
  44. //必须声明为一个数组,把该数组当参数传递给 MultiThreadedTestRunner
  45. TestRunnable[] trs = { tr1, tr2, tr3, tr4, tr5, tr6, tr7 };
  46. //不需改动
  47. MultiThreadedTestRunner mttr = new MultiThreadedTestRunner(trs);
  48. //执行MTTR和7线程
  49. mttr.runTestRunnables();
  50. }
  51. /**
  52. * 要运行多线程,首先要实现自定义的线程</br>
  53. * 如下我定义了A,B,C,D,E,F,G七个线程</br>
  54. * 注意:自定义线程必须要继承TestRunnable</br>
  55. * 并且覆盖runTest()方法
  56. *
  57. */
  58. private class ThreadA extends TestRunnable {
  59. @Override
  60. public void runTest() throws Throwable {
  61. //线程要调用的方法或者要执行的操作
  62. myCommMethod2();
  63. }
  64. }
  65. private class ThreadB extends TestRunnable {
  66. @Override
  67. public void runTest() throws Throwable {
  68. myCommMethod2();
  69. }
  70. }
  71. private class ThreadC extends TestRunnable {
  72. @Override
  73. public void runTest() throws Throwable {
  74. myCommMethod2();
  75. }
  76. }
  77. private class ThreadD extends TestRunnable {
  78. @Override
  79. public void runTest() throws Throwable {
  80. myCommMethod2();
  81. }
  82. }
  83. private class ThreadE extends TestRunnable {
  84. @Override
  85. public void runTest() throws Throwable {
  86. myCommMethod2();
  87. }
  88. }
  89. private class ThreadF extends TestRunnable {
  90. @Override
  91. public void runTest() throws Throwable {
  92. myCommMethod2();
  93. }
  94. }
  95. private class ThreadG extends TestRunnable {
  96. @Override
  97. public void runTest() throws Throwable {
  98. myCommMethod2();
  99. }
  100. }
  101. /**
  102. * 线程要调用的方法。在此方法中</br>
  103. * 实现你的多线程代码测试。
  104. * @throws Exception
  105. */
  106. public void myCommMethod2() throws Exception {
  107. System.out.println("线程===" + Thread.currentThread().getId() + "执行myCommMethod2操作开始");
  108. for (int i = 0; i <10; i++) {
  109. int a  = i*5;
  110. System.out.println(a);
  111. }
  112. System.out.println("线程===" + Thread.currentThread().getId() + "执行myCommMethod2操作结束");
  113. }
  114. }
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();
} /**
* 要运行多线程,首先要实现自定义的线程&lt;/br&gt;
* 如下我定义了A,B,C,D,E,F,G七个线程&lt;/br&gt;
* 注意:自定义线程必须要继承TestRunnable&lt;/br&gt;
* 并且覆盖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();
}
} /**
* 线程要调用的方法。在此方法中&lt;/br&gt;
* 实现你的多线程代码测试。
* @throws Exception
*/
public void myCommMethod2() throws Exception {
System.out.println("线程===" + Thread.currentThread().getId() + "执行myCommMethod2操作开始");
for (int i = 0; i &lt;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多线程测试的更多相关文章

  1. Junit使用GroboUtils进行多线程测试

    写过Junit单元测试的同学应该会有感觉,Junit本身是不支持普通的多线程测试的,这是因为Junit的底层实现上,是用System.exit退出用例执行的.JVM都终止了,在测试线程启动的其他线程自 ...

  2. 关于JUnit4无法支持多线程测试的解决方法

    转自:https://segmentfault.com/a/1190000003762719 其实junit是将test作为参数传递给了TestRunner的main函数.并通过main函数进行执行. ...

  3. 使用Spring+Junit4.4进行测试(使用注解)

    http://nottiansyf.iteye.com/blog/345819 使用Junit4.4测试 在类上的配置Annotation @RunWith(SpringJUnit4ClassRunn ...

  4. 用Spring+Junit4.4进行测试(使用注解)

    http://nottiansyf.iteye.com/blog/345819 使用Junit4.4测试 在类上的配置Annotation @RunWith(SpringJUnit4ClassRunn ...

  5. 使用Spring+Junit4.4进行测试

    http://nottiansyf.iteye.com/blog/345819 使用Junit4.4测试 在类上的配置Annotation @RunWith(SpringJUnit4ClassRunn ...

  6. testng入门教程12 TestNG执行多线程测试

    testng入门教程 TestNG执行多线程测试 testng入门教程 TestNG执行多线程测试 并行(多线程)技术在软件术语里被定义为软件.操作系统或者程序可以并行地执行另外一段程序中多个部分或者 ...

  7. TestNG多线程测试-注解方式实现

    用@Test(invocationCount = x,threadPoolSize = y)声明,invocationCount表示执行次数,threadPoolSize表示线程池大小. packag ...

  8. TestNg之XMl形式实现多线程测试

    为什么要使用多线程测试? 在实际测试中,为了节省测试时间,提高测试效率,在实际测试场景中经常会采用多线程的方式去执行,比如爬虫爬数据,多浏览器并行测试. 关于多线程并行测试 TestNG中实现多线程并 ...

  9. TestNG 多线程测试

    TestNG以注解的方式实现多线程测试 import org.testng.annotations.Test; public class TreadDemo { // invocationCount ...

随机推荐

  1. C#内存占用释放

    序言 系统启动起来以后,内存占用越来越大,使用析构函数.GC.Collect什么的也不见效果,后来查了好久,找到了个办法,就是使用 SetProcessWorkingSetSize函数.这个函数是Wi ...

  2. 使用 nodejs 和 axios 以及 cherrio 爬取天气预报

    安装依赖 引入依赖 发送请求 解析请求的返回值 以下代码可以复制直接运行,获得 7 天的天气预报 const axios = require('axios') const cheerio = requ ...

  3. python3.x使用cxfreeze将.p打包成.exe

    之前写了一个使用ffplay批量查看格式为h264的图片,每次抽帧后都要打开pycharm编译器来运行程序,然后才能正常查看图片,或者在其他没有安装python环境的电脑中运行,很不方便.为此,在网上 ...

  4. sun.misc.BASE64Decoder 替代

    加密解密经常用到sun.misc.BASE64Decoder处理,编译时会提示: sun.misc.BASE64Decoder是内部专用 API, 可能会在未来发行版中删除 解决办法: Java8以后 ...

  5. 使用GitHub(一):添加SSHkey

    使用GitHub(一):添加SSHkey 本文简单介绍使用GitHub对代码进行版本控制,包括添加SSHkey.配置Git.使用Git创建版本库并在GitHub上进行管理,主要目的是对学习内容进行总结 ...

  6. WebView:是应用程序打开web网页的UI控件后端

    public class WebViewActivity extends Activity { private WebView webView; @Override protected void on ...

  7. legend3---lavarel中使用qq邮箱发送邮件

    legend3---lavarel中使用qq邮箱发送邮件 一.总结 一句话总结: 第一步:配置邮箱做服务器,比如qq邮箱,网易163邮箱 第二步:配置lavarel的配置文件 第三部:写邮件发送代码就 ...

  8. linux查看端口被那个进程占用

    linux下遇到端口被暂用了 需要知道是哪个进程 比如80端口 可以这样 netstat -tunlp|

  9. C#程序自动安装数字证书

    using System.Security.Cryptography.X509Certificates; MessageBox.Show("开始"); //添加个人证书 X509C ...

  10. OpenStack 实现技术分解 (6) 通用库 — oslo_log

    目录 目录 前文列表 扩展阅读 日志级别 oslolog 初始化设置 DEMO oslolog 的相关配置项 oslolog 的日志级别 oslolog 的使用技巧 推荐使用 LOGdebug 的地方 ...