码迷,mamicode.com
首页 > 编程语言 > 详细

多线程单元测试

时间:2015-07-06 14:09:49      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:

  1. package com.xiw.core;
  2. import org.junit.runner.RunWith;
  3. import org.springframework.test.context.ContextConfiguration;
  4. import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
  5. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  6. /**
  7. * 单元测试基类
  8. *
  9. */
  10. @RunWith(SpringJUnit4ClassRunner.class)
  11. @ContextConfiguration(locations = {"classpath:beans.xml"})
  12. public abstract class BaseServiceTest extends AbstractTransactionalJUnit4SpringContextTests {
  13. }

  1. package com.xiw.core;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import net.sourceforge.groboutils.junit.v1.MultiThreadedTestRunner;
  5. import net.sourceforge.groboutils.junit.v1.TestRunnable;
  6. import org.junit.Test;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.test.annotation.Rollback;
  9. import com. xiw.user.autobid.AutoBidService;
  10. import com. xiw.user.business.IRechargeBiz;
  11. import com. xiw.user.dao.IRechargeDao;
  12. import com. xiw.util.Constant;
  13. import com. xiw.util.LogUtilForLog4j;

  14. public class ServiceTest extends BaseServiceTest {

  15. @Autowired
  16. private IRechargeBiz revharegBiz;
  17. @Test
  18. @Rollback(value = false)
  19. public void thread1(){
  20. // 构造一个Runner
  21. TestRunnable runner = new TestRunnable() {
  22. @Override
  23. public void runTest() throws Throwable {
  24. // 测试内容
  25. revharegBiz.update1();
  26. }
  27. };
  28. int runnerCount = 3;
  29. //Rnner数组,想当于并发多少个。
  30. TestRunnable[] trs = new TestRunnable[runnerCount];
  31. for (int i = 0; i < runnerCount; i++) {
  32. trs[i] = runner;
  33. }
  34. // 用于执行多线程测试用例的Runner,将前面定义的单个Runner组成的数组传入
  35. MultiThreadedTestRunner mttr = new MultiThreadedTestRunner(trs);
  36. try {
  37. // 开发并发执行数组里定义的内容
  38. mttr.runTestRunnables();
  39. } catch (Throwable e) {
  40. e.printStackTrace();
  41. }
  42. }
  43. }

需要增加jar包:
  1. <dependency>
  2. <groupId>junit</groupId>
  3. <artifactId>junit</artifactId>
  4. <version>4.12</version>
  5. <scope>test</scope>
  6. </dependency>
  7. <dependency>
  8. <groupId>net.sourceforge.groboutils</groupId>
  9. <artifactId>groboutils-core</artifactId>
  10. <version>5</version>
  11. </dependency>
groboutils-core需要第三方库支持:
RepositoryOpensymphony Releases
Repository urlhttps://oss.sonatype.org/content/repositories/opensymphony-releases




多线程单元测试

标签:

原文地址:http://www.cnblogs.com/wx491592452/p/ee480b1719f8404c8b0a640ed3604d2e.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!