can't inject `local.server.port` into a @Configuration so it can be wired into a @Component

See original GitHub issue

So 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:closed
  • Created 5 years ago
  • Comments:11 (8 by maintainers)

github_iconTop GitHub Comments

37reactions
knoobiecommented, Mar 25, 2020

I stumbled across this ‘issue’ as well - got it working if somebody needs it in the future.

  @Lazy // allows for initialization after the server port is known
  @TestConfiguration(proxyBeanMethods = false)
  static classTestConfiguration {

    @Bean
    public YourClass yourBean(@Value("${local.server.port}") int port) {
      //...
    }
  }
13reactions
wilkinsonacommented, Oct 17, 2018

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found