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

1

6

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

Panelboard answered 27/1, 2021 at 14:28 Comment(0)
G
5

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.

Gaspar answered 28/1, 2021 at 9:51 Comment(2)
by chance do you have a link to an example?Newlywed
@JamesBowler take a look athttps://github.com/spockframework/spock/blob/master/spock-spring/src/test/groovy/org/spockframework/spring/SpringSpecInheritance.groovy just put all tests in BaseSpec and then add subclasses like SpringSpecInheritance for each variation.Durance

© 2022 - 2024 — McMap. All rights reserved.