[SOLVED] JUnit @RepeatedTest for concurrent execution

Issue

I’ve read following article and I understand that I can ask junit to execute test several times by adding following annotation:

@RepeatedTest(value = 3, name = RepeatedTest.LONG_DISPLAY_NAME)

But I want to start these 3 time in parallel.
Is it posssible?

P.S. I want to check that tests pass correctly for 10+ threads

Solution

Nowadays this is possible by leveraging the (experimental) Parallel Execution feature. For this you need to add a junit-platform.properties file to your test resource directory. You also need to add the @Execution(ExecutionMode.CONCURRENT) to your tests. See the examples below:

junit-platform.properties:

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

Test

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

class ParallelRepeatTest {

    @Execution(ExecutionMode.CONCURRENT)
    @RepeatedTest(10)
    void testVilocDecodingWithSabotagedFalse_usingMqttPlug() throws Exception {
        System.out.println("Started");
        Thread.sleep(5000);
        System.out.println("Finished");
    }
}

If you need to tweak the threading settings, refer to the junit5 documentation linked above.

Answered By – Seanvd

Answer Checked By – Robin (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *