junit是沒法進行併發測試,可是又有須要併發測試的場景怎麼辦呢?此時能夠藉助一個插件(Groboutils Core)來完成這種功能。
maven倉庫地址:點我直達數組
<!-- 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(); } }