1

i am having an issue to understand how can i run all the unit tests in my boot spring application(kotlin) in parallel while the springBootTests and the dataJpaTests will run one after the other(becouse they are failing due to shared context where running in parallel), my application structure is separated to different models and each model have it's unit,springBootTests and dataJpaTets, like this:

-module 1:

  • unit test 1

  • unit test 2

  • data jpa test 1

  • data jpa test 2

  • spring boot test1

-module 2:

  • unit test 1

  • unit test 2

  • unit test 3

  • data jpa test 1

  • spring boot test1

i used the following properties from https://junit.org/junit5/docs/current/user-guide/#writing-tests-parallel-execution:

junit.jupiter.execution.parallel.enabled = true

junit.jupiter.execution.parallel.mode.default = concurrent //tests in each class run in parallel

junit.jupiter.execution.parallel.mode.classes.default = concurrent //classes run in parallel

but it is not helping becouse there is no way to exclude the springBoot and dataJpa tests from the parallization.

also,i tried to put the @Execution(SAME_THREAD) on all the dataJpa and springBoot test but still the classes itself runed in parallel and test was colliding

*i use --test *test commend to run all the tests together

  • #1 java, maven, spring boot and junit version? #2 Do you only need to exclude some test class from the parallel execution? #3 It helps you to run first the parallel test (excluding some tests) and after that just run the excluded tests? #4 Are you using shell with maven or IDE to run the tests? – JRichardsz Jul 09 '22 at 13:28
  • i am using kotlin 1.5 and gradel(not maven) , spring boot version is 2.6.8 and junit 5. it doesn't meter for me in which order to run the tests, only that i will be able parallelize most of them. and because i know i cant parallelize spring boot and data jpa tests i decided to try to parallelize only the unit tests – sigal notovich Jul 25 '22 at 12:10

1 Answers1

0

Spring uses Junit

By default, JUnit Jupiter tests are run sequentially in a single thread.

Running tests in parallel — for example, to speed up execution — is available as an experimental feature since version 5.3

Source: https://junit.org/junit5/docs/current/user-guide/#writing-tests-parallel-execution

Using pure Junit

@Execution(ExecutionMode.CONCURRENT)
class MyTest {

    @Test
    void test1() throws InterruptedException {
        Thread.sleep(1000);
        System.out.println("Test1 " + Thread.currentThread().getName());
    }

    @Test
    void test2() throws InterruptedException {
        Thread.sleep(1000);
        System.out.println("Test 2! " + Thread.currentThread().getName());
    }
}

source:

Using spring + maven + junit

<build>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.22.2</version>
        <configuration>
            <parallel>methods</parallel>
            <useUnlimitedThreads>true</useUnlimitedThreads>
        </configuration>
    </plugin>
</build>
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = Spring5JUnit4ConcurrentTest.SimpleConfiguration.class)
public class Spring5JUnit4ConcurrentTest implements ApplicationContextAware, InitializingBean {

    @Configuration
    public static class SimpleConfiguration {}

    private ApplicationContext applicationContext;

    private boolean beanInitialized = false;

    @Override
    public void afterPropertiesSet() throws Exception {
        this.beanInitialized = true;
    }

    @Override
    public void setApplicationContext(
      final ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @Test
    void test1A() {
        System.out.println(Thread.currentThread().getName()+" => test1A");
    }
    
    @Test
    void test1B() {
        System.out.println(Thread.currentThread().getName()+" => test1B");
    }
    @Test
    void testC() {
        System.out.println(Thread.currentThread().getName()+" => test1C");
    }  

}

Sources:

Simple tests are "almost parallel"

According to this and my tests, simple junit tests are executed almost parallel

public class Hello1Test {
    @Test
    public void myTest() throws Exception {
      System.out.println(new Date());
      assertTrue(true);
    }
}

mvn test

enter image description here

NOTE: If you add some Thread related in the test, they are executed sequentially

  @Test
  public void myTest() throws Exception {
    System.out.println(new Date());
      Thread.sleep(2000);
      assertTrue(true);
  }

enter image description here

Exclude some tests

According to this you could use -Dtest to pick or exclude specific tests

  • mvn test -q
    • run all tests
  • mvn test -q -Dtest='Hello1*'
    • run only test with name Hello1*
  • mvn test -q -Dtest='!Hello1*, !Hello2*'
    • run all tests except Hello1* and Hello2*

enter image description here

Tips

General Grievance
  • 4,555
  • 31
  • 31
  • 45
JRichardsz
  • 14,356
  • 6
  • 59
  • 94
  • it seems that your assumption is wrong. – Mr.Q Oct 03 '22 at 16:55
  • Tests are NOT run in parallel by default. You have to explicitly enable parallel execution. Your test does not prove they are parallel: such simple tests will take only a tiny fraction of a second and it is therefore quite expected that they all run in the same second even if they are run sequentially. – julaine Oct 14 '22 at 11:27
  • You are right. I already updated it – JRichardsz Oct 14 '22 at 20:32