0

I'm using Spring Boot 2. I have

@Value("${my.read.timeout:-1}")
private Integer timeout;

@Bean
public RestTemplate getRt() {
    RestTemplate rt = new RestTemplate(getFactory());
    return rt;
}

private HttpComponentsClientHttpRequestFactory getFactory() {
    HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
    factory.setReadTimeout(timeout);
    return factory;
}

The problem is "-1" is not a valid default value for the timeout setting. If no explicit property is defined I would like to use whatever the default read timeout is. How do I do this?

Dave
  • 15,639
  • 133
  • 442
  • 830
  • if (timeout == null || timeout == -1) { factory.setReadTimeout(DEFAULT_TIMEOUT); } else { factory.setReadTimeout(timeout); } – Kavindu Gayan Sep 01 '23 at 08:21
  • I might go this way, the inconvenience with this is, to satisfy test coverage, I have to write multiple tests for each situation (timeout == null, timeout != null, tineout == -1, timeout != -1), whereas if I have a single expression in the @Value clause, I only need to have one unit test to satisfy coverage. – Dave Sep 01 '23 at 13:09

0 Answers0