-
Notifications
You must be signed in to change notification settings - Fork 41.2k
Mapped port can only be obtained after the container is started when trying to resolve @ConditionalOnProperty #41664
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
The scenario here is quite different as you're querying the value of a property in a condition. Conditions are evaluated very early – while the bean factory is being populated and before any beans exist – and this is too early for the infrastructure that automatically starts a container when a property that it provides is used. If you want to be able to use @SpringBootTest
@Testcontainers
class Gh41664ApplicationTests {
@Container
private static MockAuthServerTestContainer container = new MockAuthServerTestContainer().withNetwork(Networks.BRIDGE);
@DynamicPropertySource
static void setContainerProperties(DynamicPropertyRegistry registry) {
registry.add("authentication.url", () -> String.format("http://localhost:%d/", container.getFirstMappedPort()));
}
@Test
void contextLoads() {
}
} Given your use of |
Yes, you are right. It is working like this. My goal here was other. I wanted to be able to avoid having this container and the DynamicPropertySource declared in all the test classes where this is needed. I wanted to use the approach with the configuration so that I can reuse the same setup everywhere, without explicitly duplicating that code everywhere... but as you mentioned it is not possible. With other containers that do not influence beans via conditional checking everything is working ok. So, I guess for these scenarios I have to stick with this. With the @ImportTestContainers approach I was even able to define my own annotation that provides all the setup to the test class. e.g.
Adding @WithMyContainer to any test class would just provide the container to that test class. Thank you for your answer! |
You can use an interface for this:
A test class can then implement this interface:
This allows the configuration to be shared while also allowing Testcontainers to manage the container lifecycle. I don't think there's anything that we can do in Spring Boot to improve this so I'll close this one. |
This doesn't seem to be working since |
I would guess that's due to context caching and that the affected tests have otherwise identical configuration. You may need to use |
Using I also believe that having a mechanism to reuse container configurations for those containers that dont't rely on |
I have an issue related to #40585 . I am using Spring Boot 3.3.2, which should have the fix, however maybe my scenario is slightly different.
I have a bean which has
@ConditionalOnProperty("x")
and a test containers configuration which uses dynamic property source to inject the exact same proprty "x". What happens is that the conditional tries to resolve the property before the container is started and cannot access the mapped port.Container configuration
Bean configuration
Test usage
The behavior is that trying to resolve the property in order to determine bean creation happens before the container is started and the port cannot be retrieved: "Mapped port can only be obtained after the container is started"
The text was updated successfully, but these errors were encountered: