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

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

不言
Release: 2019-03-14 11:08:47
forward
3222 people have browsed it

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>
Copy after login

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();
    }
  }
Copy after login

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!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template