Wednesday 25 August 2021

Junit5: Parallel test execution

By default, all the junit tests run sequentially by a single thread. When tests are independent, running them parallel makes the execution faster.

 

You should set following parameters to run the tests in parallel.

 

junit.jupiter.execution.parallel.enabled = true

junit.jupiter.execution.parallel.mode.default = concurrent

junit.jupiter.execution.parallel.config.strategy=fixed

junit.jupiter.execution.parallel.config.fixed.parallelism=4

 

junit.jupiter.execution.parallel.enabled

You should set above property to true, to enable parallel execution.

 

junit.jupiter.execution.parallel.mode.default

Above statement is used to set the execution mode. There are two modes available.

 

Mode

Description

same_thread

Force execution in the same thread used by the parent. For example, when used on a test method, the test method will be executed in the same thread as any @BeforeAll or @AfterAll methods of the containing test class.

concurrent

Execute concurrently unless a resource lock forces execution in the same thread.

 

You can even set the mode using @Execution annotation.

 

@Execution(ExecutionMode.CONCURRENT)

class ParallelTestDemo1 {

 

}

 

Configure parallelism strategy and thread pool size

ParallelExecutionConfigurationStrategy interface is used to configure parallel execution strategy. Currently junit platform provides two implementations of strategy out of the box. If you want, you can implement custom strategy also.

 

Below table summarizes the strategies provided by junit platform.

 

Strategy

Description

dynamic

Computes the desired parallelism based on the number of available processors/cores multiplied by the junit.jupiter.execution.parallel.config.dynamic.factor configuration parameter (defaults to 1).

 

If no configuration strategy is set, JUnit Jupiter uses the dynamic configuration strategy with a factor of 1.

fixed

Uses the mandatory junit.jupiter.execution.parallel.config.fixed.parallelism configuration parameter as the desired parallelism.

custom

Allows you to specify a custom ParallelExecutionConfigurationStrategy implementation via the mandatory junit.jupiter.execution.parallel.config.custom.class configuration parameter to determine the desired configuration.

 

Find the below working application.

 

Step 1: Configure following properties in src/test/resources/junit-platform.properties file.

junit.jupiter.execution.parallel.enabled = true
junit.jupiter.execution.parallel.config.strategy=fixed
junit.jupiter.execution.parallel.config.fixed.parallelism=4

 

Step 2: Define ParallelTestDemo class.

 

ParallelTestDemo.java

package com.sample.app;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;

@Execution(ExecutionMode.CONCURRENT)
public class ParallelTestDemo {
	
	private static int TIME_TO_SLEEP = 5000;
	
    @Test
    void test1() throws InterruptedException {
        Thread.sleep(TIME_TO_SLEEP);
        System.out.println("Test1: " + Thread.currentThread().getName());
    }

    @Test
    void test2() throws InterruptedException {
        Thread.sleep(TIME_TO_SLEEP);
        System.out.println("Test2: " + Thread.currentThread().getName());
    }
    
    @Test
    void test3() throws InterruptedException {
        Thread.sleep(TIME_TO_SLEEP);
        System.out.println("Test3: " + Thread.currentThread().getName());
    }
    
    
    @Test
    void test4() throws InterruptedException {
        Thread.sleep(TIME_TO_SLEEP);
        System.out.println("Test4: " + Thread.currentThread().getName());
    }
    
    @Test
    void test5() throws InterruptedException {
        Thread.sleep(TIME_TO_SLEEP);
        System.out.println("Test5: " + Thread.currentThread().getName());
    }
}

 

Run above test class.

 

 


You can observe that 4 test methods start execution parallelly.

 

Once all the test methods executed, you will see below messages in console.

Test5: ForkJoinPool-1-worker-1
Test3: ForkJoinPool-1-worker-0
Test2: ForkJoinPool-1-worker-3
Test1: ForkJoinPool-1-worker-2
Test4: ForkJoinPool-1-worker-3

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment