You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using @TestInstance(PER_CLASS) with Spring Boot, Quarkus, or similar frameworks, non-static @MethodSource factory methods may depend on static @Container fields being started. These factory methods run after postProcessTestInstance but before beforeAll, so the containers aren't started yet.
Example:
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@Testcontainers@SpringBootTestclassUserServiceTest {
@ContainerstaticPostgreSQLContainer<?> postgres = newPostgreSQLContainer<>("postgres:16");
@AutowiredUserServiceuserService;
// Non-static @MethodSource is possible with PER_CLASS.// Without the fix, postgres is not started yet when this runs.Stream<Arguments> userArguments() {
returnuserService.findAllRoles(
postgres.getJdbcUrl(), postgres.getUsername(), postgres.getPassword()
).stream().map(Arguments::of);
}
@ParameterizedTest@MethodSource("userArguments")
voidshould_create_user_with_role(Stringrole) {
// ...
}
}
With PER_CLASS, postProcessTestInstance is called exactly once per test class (same as beforeAll), so moving shared container startup there doesn't change the lifecycle guarantees. It just makes them available earlier, during @MethodSource resolution and for other TestInstancePostProcessor extensions.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
When using
@TestInstance(PER_CLASS)with Spring Boot, Quarkus, or similar frameworks, non-static@MethodSourcefactory methods may depend on static@Containerfields being started. These factory methods run afterpostProcessTestInstancebut beforebeforeAll, so the containers aren't started yet.Example:
With
PER_CLASS,postProcessTestInstanceis called exactly once per test class (same asbeforeAll), so moving shared container startup there doesn't change the lifecycle guarantees. It just makes them available earlier, during@MethodSourceresolution and for otherTestInstancePostProcessorextensions.I submitted a PR with an implementation: #11703
I should have opened this discussion first, apologies. Happy to hear your thoughts.
Beta Was this translation helpful? Give feedback.
All reactions