10

I am upgrading Spring Boot from 1.3 to 1.5. For upgrading to 1.5 I have replaced

@SpringApplicationConfiguration(classes = TestConfig.class) @WebIntegrationTest

with

@SpringBootTest(classes = TestConfig.class)

Also, I am using

@Value("${local.server.port}") protected int port;

to get port number defined in application.properties file. I further use this port number to build a REST URL.

But after the upgrade I am getting the error below whereas the same works fine with 1.3 Spring Boot Test.

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'local.server.port' in value "${local.server.port}" at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174)

Am I missing any changes that I need to do for this to work.

Meena Chaudhary
  • 9,909
  • 16
  • 60
  • 94

4 Answers4

12

You have to provide a value for webEnvironment. In your case DEFINED_PORT like this

@SpringBootTest(classes = App.class, webEnvironment = WebEnvironment.DEFINED_PORT)
public class YourTest {

  @LocalServerPort           // shorthand for @Value("${local.server.port}")
  private Integer port;
  ...
}

For details see: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html#boot-features-testing-spring-boot-applications

newur
  • 490
  • 4
  • 12
4

Adding another alternate solution which I had elsewhere.

I had configured

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

and

@RunWith(SpringRunner.class)
@SpringBootTest(classes = App.class, webEnvironment = WebEnvironment.RANDOM_PORT)
public class YourTest {

  @LocalServerPort           // shorthand for @Value("${local.server.port}")
  private Integer port;
  ...
}

Thinking that was it, and still getting this error even when specifying web environment etc. My ${local.server.port} seemed to be always null.

After some time, I noticed that my Spring Boot startup message contained no notion of the port it was using, so apparently it really didn't listen to any port at all - which explained why it was null in the first place. Adding actual container implementation dependency:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jetty</artifactId>
    </dependency>

Caused this to appear on my logs:

2019-02-26 18:45:47.231  INFO 12504 --- [           main] o.s.b.web.embedded.jetty.JettyWebServer  : Jetty started on port(s) 43132 (http/1.1) with context path '/'

after which local.server.port and @LocalServerPort would also work.

eis
  • 51,991
  • 13
  • 150
  • 199
1

For me the problem was that there was alternative @Configuration class(es) in my other test(s) like this:

@Configuration
public class ReadPropertiesConfiguration {
    @Bean
    PropertyPlaceholderConfigurer propConfig() {
        PropertyPlaceholderConfigurer placeholderConfigurer = new PropertyPlaceholderConfigurer();
        placeholderConfigurer.setLocation(new ClassPathResource("application.properties"));
        return placeholderConfigurer;
    }
}

and @SpringBootApplication of the app was picking that up due to its @ComponentScan, and for some reason it resulted in this problem. When adding exclusion for those and/or replacing them with other solutions things started again to work without problems.

I don't know the root cause why this happens, but that might be your issue as well.

eis
  • 51,991
  • 13
  • 150
  • 199
0

First make sure the property is correctly spelled in the properties file. As i did just few days back using spring-boot-1.5.2 & it works.

Or

You need to add

@PropertySource("classpath:application.properties")

to your class, so it will pick your configurations.

If you need different configurations for test you can add

@TestPropertySource(locations="classpath:test.properties")

Refer @Value not work on Spring Boot Test

Community
  • 1
  • 1
Gurpreet Singh
  • 380
  • 4
  • 9