spring-boot
spring-boot copied to clipboard
Consider automatically applying @DirtiesContext to Testcontainer tests
See #38176 for an example of a problem caused by a mismatch of the test containers and spring lifecycle. It would be nice if we could prevent users from hitting the same problem.
@philwebb This seems interesting, can I take this issue?
After a quick look, I think I can solve it by creating a class that inherits from 'AbstractTestExecutionListener'. Something like 'TestcontainersTestExecutionListener' similar to 'DirtiesContextTestExecutionListener'... Is it okay to proceed in this way?
@kth496 I'd like to discuss this issue a little more as a team before we make any concrete decisions.
@philwebb What do you mean by
automatically applying @DirtiesContext to Testcontainer tests
If you mean that any test class that is annotated with @Testcontainers will be automatically marked with @DirtiesContext (even if not explicitly marked), then I think this is not a good idea. If you mean something else, then sorry for pinging.
I think, the lifecycle mismatch between Spring context shutdown and Testcontainers shutdown should be fixed in some other way. In a way that will make sure that containers are not stopped before Spring contexts are fully stopped.
This is especially important for containers with DB with migration applied. Usually such container is started only once per all integration tests. Marking every integration test with @DirtiesContext, while is cleaner solution, will force all DB migrations to run again and again (not speaking about context startup time itself).