spring-boot icon indicating copy to clipboard operation
spring-boot copied to clipboard

Consider automatically applying @DirtiesContext to Testcontainer tests

Open philwebb opened this issue 2 years ago • 3 comments

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 avatar Nov 06 '23 19:11 philwebb

@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 avatar Jan 22 '24 15:01 kth496

@kth496 I'd like to discuss this issue a little more as a team before we make any concrete decisions.

philwebb avatar Jan 22 '24 18:01 philwebb

@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).

xak2000 avatar Feb 26 '24 18:02 xak2000