Reputation: 345
I'm trying to implement integration testing in my app and have test class like that:
@ExtendWith(value={MyDockerExtension.class})
@ExtendWith(value={SpringExtension.class})
@WebAppConfiguration
@ContextConfiguration(classes={...})
@TestInstance(TestInstance.LifeCycle.PER_CLASS)
public class TestClass{ ... }
Is there any way to make MyDockerExtension execute some code, before whole SpringExtension start working and generate whole Context with Configurationc classes?
I've heard that order in which we declare extensions is the key, but sadly MyDockerExtension that implements BeforeAllCallback, AfterAllCallback
executes right before test method and after whole context is loaded. In that situation it's to late to start containers with docker, becuase since whole context is loaded my app already tried to connect to the container.
Upvotes: 3
Views: 1141
Reputation: 187
Old post but didn't see any accepted answer to it. I would use TestContainers instead. That way you can spin up the application separate from the test code.
Upvotes: 0
Reputation: 51130
At first I was skeptical about the order being fixed but you're correct:
Extensions registered declaratively via
@ExtendWith
will be executed in the order in which they are declared in the source code.
Regarding the MyDockerExtension
, you may want to look at the extension point TestInstancePostProcessor
, which is called before @BeforeAll
. SpringExtension
implements it and I guess it's there where it sets up the application context. If you also implement it, you should be able to act before it does.
Upvotes: 4