Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I run a `@SpringBootTest` class multiple times with different configurations?

I have an integration test that's using @SpringBootTest to spin up a Spring application context that's testing a simple Spring Boot app. I'm using Spock for writing the tests, my build tool is Maven.

I'm looking for a way to run the same test class multiple times with different configurations for the test (I have a set of config options and I need to ensure consistent behavior for a certain permutation of config options). The first idea I had was to use profiles to define the exact permutation, maybe it could also work by using @TestPropertySource in some way. However, I don't see any way to run a test class multiple times, using different configurations each time.

I know that I could run all tests with a given profile, but in my case I want to only apply the different configs to certain test classes.
I can also use a where block to repeat spock tests as described here, but that doesn't allow me to switch spring configurations for each run

like image 852
l7r7 Avatar asked Oct 14 '22 22:10

l7r7


1 Answers

The easiest way is to use simple subclasses, i.e., you define all your tests in an abstract base class and then subclass it for every variation and add the necessary annotations to the subclasses. This approach works well if you only have a limited set of variations, and provides good reporting feedback as every variation is reported as its own specification.

like image 89
Leonard Brünings Avatar answered Oct 18 '22 14:10

Leonard Brünings