Junit 5 Test Case Parallel Execution

By default, All the test cases written in JUnit 5 runs sequentially in a single thread. we can speed up the execution by running the test cases parallelly. to avail of this feature, JUnit 5 provides us @Execution annotation to run test case parallelly. @Execution annotation takes execution mode as parameter, and its values are SAME_THREAD which is default or CONCURRENT


package com.example.junit5.sujan;

public class AppleCalculator {
    public int addApple(int appleBag1, int appleBag2) throws InterruptedException {
        return appleBag1 + appleBag2;
    }
}

package com.example.junit5.sujan;

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

import static org.junit.jupiter.api.Assertions.assertEquals;

@Execution(ExecutionMode.SAME_THREAD)
class AppleCalculatorTest {
    @Test
    void addAppleTestCase1() throws InterruptedException {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
    }

    @Test
    void addAppleTestCase2() throws InterruptedException {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
    }

    @Test
    void addAppleTestCase3() throws InterruptedException {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
    }

    @Test
    void addAppleTestCase4() throws InterruptedException {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
    }

    @Test
    void addAppleTestCase5() throws InterruptedException {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
    }
}

plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}

 

ExecutionMode CONCURRENT allow junit 5 to concurrent execution with other test cases.

 

package com.example.junit5.sujan;

public class AppleCalculator {
    public int addApple(int appleBag1, int appleBag2) throws InterruptedException {
        return appleBag1 + appleBag2;
    }
}

 

package com.example.junit5.sujan;

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

import static org.junit.jupiter.api.Assertions.assertEquals;

@Execution(ExecutionMode.CONCURRENT)
class AppleCalculatorTest {
    @Test
    void addAppleTestCase1() throws InterruptedException {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
    }

    @Test
    void addAppleTestCase2() throws InterruptedException {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
    }

    @Test
    void addAppleTestCase3() throws InterruptedException {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
    }

    @Test
    void addAppleTestCase4() throws InterruptedException {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
    }

    @Test
    void addAppleTestCase5() throws InterruptedException {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
    }
}
plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}

 

if we have multiple test cases classes and all of them needs to run parallely then annotating each class with

@Execution(ExecutionMode.CONCURRENT) would be a lot of boiler plate code and is not best practice. 

to avoid this, junit provides a configuration properties.

junit.jupiter.execution.parallel.enabled to true in junit-platform.properties.

junit.jupiter.execution.parallel.enabled = true

to run concurrent threads we need to provide 1 more property.

package com.example.junit5.sujan;

public class AppleCalculator {
    public int addApple(int appleBag1, int appleBag2) throws InterruptedException {
        return appleBag1 + appleBag2;
    }
}
package com.example.junit5.sujan;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

class AppleCalculatorTest {
    @Test
    void addAppleTestCase1() throws InterruptedException {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
    }

    @Test
    void addAppleTestCase2() throws InterruptedException {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
    }

    @Test
    void addAppleTestCase3() throws InterruptedException {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
    }

    @Test
    void addAppleTestCase4() throws InterruptedException {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
    }

    @Test
    void addAppleTestCase5() throws InterruptedException {
        AppleCalculator appleCalculator = new AppleCalculator();
        assertEquals(2, appleCalculator.addApple(1, 1), "1 apple + 1 apple is 2 apple");
    }
}
junit.jupiter.execution.parallel.enabled=true
junit.jupiter.execution.parallel.mode.default=concurrent
plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}

 

follow us on