can't inject `local.server.port` into a @Configuration so it can be wired into a @Component
See original GitHub issueSo the short of my problem is, I’m trying to make a graphql client library, and test to see that it’s working. @LocalServerPort isn’t becoming available until @Test in Junit 5 in my project. I can’t quite repro this in a demo project, but I believe this project should work, as is
@SpringJUnitConfig
@SpringBootTest( webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT )
class ActuatorApplicationTest {
@Autowired String url;
@Test
void port() {
Assertions.assertNotNull( url );
}
@TestConfiguration
static class Config {
@Bean
String url( @LocalServerPort int port ) {
return "http://localhost:" + port;
}
}
}
This is my real code right now…
@Configuration
class ApolloConfig {
@Bean
@Profile( "!test" )
static ApolloClient.Builder prodClient( @Value( "${phdb.endpoint}" ) String graphqlEndpoint ) {
return ApolloClient.builder().serverUrl( graphqlEndpoint );
}
@Bean
@Profile( "test" )
static ApolloClient.Builder testClient(@Value( "${local.server.port}" ) int port ) {
return ApolloClient.builder();
}
}
failure is
Unexpected exception during bean creation; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'local.server.port' in value "${local.server.port}"
Issue Analytics
- State:
- Created 5 years ago
- Comments:11 (8 by maintainers)
Top Results From Across the Web
Unable to set runtime Local Server Port in Spring Boot Test 1.5
In integration testing I want to fetch the runtime port number of the web server(note: TestRestTemplate is not useful in my case.). There...
Read more >Troubleshoot configuration server issues - Azure
Ensure that the source machine can reach the configuration server through port 443. Check whether any firewall rules on the source machine ...
Read more >Get the Running Port in Spring Boot - Baeldung
In this article, we've discussed different approaches to obtain the fixed and random port at runtime.
Read more >Cannot connect to a database | DataGrip Documentation
Check your network settings. Databases can work locally, on a server, or in the cloud. For server and cloud databases ...
Read more >KNIME Components Guide
Setup component dialog. Here you can: Change the component name. Add input and output ports by clicking the Add… button and selecting the...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I stumbled across this ‘issue’ as well - got it working if somebody needs it in the future.
The port isn’t guaranteed to be known until the embedded container has started and bound its connector. You can inject it into a test class by which time the context will have been refreshed. Alternatively, if you need it earlier you can listen for the
WebServerInitializedEvent. You can’t inject it into any component as the port is not known in time.