Search code examples
springspring-bootjunitspring-testspring-async

How to run Spring boot tests concurrently?


I have multiple Spring Tests, that are executed one-by one, while I would like to run them concurrently.

Code sample:

@SpringBootTest
@RunWith(Suite.class)
@Suite.SuiteClasses({
     Test1.class,
     Test2.class,
     Test3.class,
     ...
})
public class SuiteStarter { }
    

@RunWith(SpringRunner.class)
@SpringBootTest
public class Test1 {
       @Autowired | @Value fields;

       @org.junit.Test
       public void test1_1() {
            Assertions.assertThat(something1());
        }
       @Test
       public void test1_2() {
            Assertions.assertThat(something2());
        }
}
       ...

Is there something similar to just annotating Suite class with @Async? I have hundreds of test classes with multiple methods, so the best solution would be just to change SuiteRunner class, with as little changes as possible, because I'm scared of breaking tests.

I have the same application context for all tests, if it helps.

Running JUnit Test in parallel on Suite Level? links provided there are dead and answers were not accepted. I do not need maven parallel run like in this link Running junit tests in parallel in a Maven build?. I also do not need answer in here Running Tests in Parallel with Junit because it features an experimental feature and code looks ugly too.


Solution

  • So the easiest and cleanest way I found is to add this dependecy to Maven

    <!-- https://mvnrepository.com/artifact/com.mycila/mycila-junit -->
    <dependency>
        <groupId>com.mycila</groupId>
        <artifactId>mycila-junit</artifactId>
        <version>1.4.ga</version>
        <scope>test</scope>
    </dependency>
    

    And just change SuiteRunner class

    @SpringBootTest
    @RunWith(ConcurrentSuiteRunner.class)
    @Concurrency(value = 12)
    @Suite.SuiteClasses({
    ...
         
    

    Results

    Before: 250 seconds

    After: 60 seconds