SSH框架学习(八、Junit+GroboUtils进行多线程测试)

原文转载:http://blog.csdn.net/wuyt2008/article/details/8275125

Junit4不能模拟多线程的情况,需要其他支持,我用的是GroboUtils,最新版本5,下载地址:http://groboutils.sourceforge.net/downloads.html


GroboUtils测试的代码是用网上抄来的,来源:http://www.coderli.com/multi-thread-junit-grobountils

UserDAOImplTest的代码

[java]  view plain copy
  1. package demo.myssh.dao.impl;  
  2.   
  3. import net.sourceforge.groboutils.junit.v1.MultiThreadedTestRunner;  
  4. import net.sourceforge.groboutils.junit.v1.TestRunnable;  
  5. import org.junit.Before;  
  6. import org.junit.Test;  
  7. import org.junit.runner.RunWith;  
  8. import org.springframework.beans.factory.annotation.Autowired;  
  9. import org.springframework.beans.factory.annotation.Qualifier;  
  10. import org.springframework.test.annotation.Repeat;  
  11. import org.springframework.test.context.ContextConfiguration;  
  12. import org.springframework.test.context.junit4.*;  
  13. import demo.myssh.dao.IUserDAO;  
  14. import demo.myssh.model.User;  
  15.   
  16. @RunWith(SpringJUnit4ClassRunner.class)  
  17. @ContextConfiguration({ "file:WebRoot/WEB-INF/applicationContext.xml" })  
  18. public class UserDAOImplTest {  
  19.   
  20.     @Autowired  
  21.     @Qualifier("iUserDAO")  
  22.     private IUserDAO userDao;  
  23.   
  24.     @Test  
  25.     @Repeat(2)  
  26.     public void MultiRequestsTest() {  
  27.         // 构造一个Runner  
  28.         TestRunnable runner = new TestRunnable() {  
  29.             @Override  
  30.             public void runTest() throws Throwable {  
  31.                 // 测试内容  
  32.                 // System.out.println("a");  
  33.                 userDao.save(new User("aa""bb""cc"));  
  34.             }  
  35.         };  
  36.         int runnerCount = 2;  
  37.         // Rnner数组,相当于并发多少个。  
  38.         TestRunnable[] trs = new TestRunnable[runnerCount];  
  39.         for (int i = 0; i < runnerCount; i++) {  
  40.             trs[i] = runner;  
  41.         }  
  42.         // 用于执行多线程测试用例的Runner,将前面定义的单个Runner组成的数组传入  
  43.         MultiThreadedTestRunner mttr = new MultiThreadedTestRunner(trs);  
  44.         try {  
  45.             // 开发并发执行数组里定义的内容  
  46.             mttr.runTestRunnables();  
  47.         } catch (Throwable e) {  
  48.             e.printStackTrace();  
  49.         }  
  50.     }  
  51. }  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值