Junit借助Groboutils Core进行并发测试

发表于:2019-3-27 11:18

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:不知怎么办才好    来源:思否

  背景
  junit是无法进行并发测试,但是又有需要并发测试的场景怎么办呢?此时可以借助一个插件(Groboutils Core)来完成这种功能。
  maven仓库地址:点我直达
  实现
  第一步:在项目的pom.xml中加入依赖
   <!-- https://mvnrepository.com/artifact/net.sourceforge.groboutils/groboutils-core -->
  <dependency>
  <groupId>net.sourceforge.groboutils</groupId>
  <artifactId>groboutils-core</artifactId>
  <version>5</version>
  <scope>test</scope>
  </dependency>
  第二步:在单测中进行代码编写
   @Test
  public void testConcurrentInitOrBind() {
  // mock一个返回
  doReturn(Lists.newArrayList(userMemberCard)).when(operateCardDao)
  .queryCardByRegisterMobileAndTenantId(anyString(), anyLong());
  TestRunnable runner = new TestRunnable() {
  // 在runTest方法中填写自己的测试方法
  @Override
  public void runTest() throws Throwable {
  InitCardResVo resVoFirst = operateCardService.initOrBindCard(requestVo);
  System.out.println("result resVoFirst is:" + resVoFirst.toString());
  }
  };
  // 一个数组,代表并发个数。此处并发5个
  TestRunnable[] trs = new TestRunnable[5];
  for (int i = 0; i < 5; i++) {
  trs[i] = runner;
  }
  MultiThreadedTestRunner mttr = new MultiThreadedTestRunner(trs);
  try {
  mttr.runTestRunnables();
  } catch (Throwable ex) {
  ex.printStackTrace();
  }
  }

      上文内容不用于商业目的,如涉及知识产权问题,请权利人联系博为峰小编(021-64471599-8017),我们将立即处理。
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号