Home  >  Article  >  Java  >  Implementation method of Junit using Groboutils Core for concurrent testing (detailed steps)

Implementation method of Junit using Groboutils Core for concurrent testing (detailed steps)

不言
不言forward
2019-03-14 11:08:473158browse

The content of this article is about the analysis of the MVC architecture in the PHP framework (with examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

junit cannot perform concurrent testing, but what should I do if there are scenarios that require concurrent testing? At this time, you can use a plug-in (Groboutils Core) to complete this function.
maven warehouse address: Click here to go directly

Implementation

The first step: Add dependencies to the pom.xml of the project

<!-- 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>

Step 2: Write code in single test

@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();
    }
  }

The above is the detailed content of Implementation method of Junit using Groboutils Core for concurrent testing (detailed steps). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete