0

I have configured my server to support TLS and everything works fine and all test cases pass. But when i decide to take the value from my .yaml i used @Value and the server still works fine but the cases case has error like

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'ServerConfig': Unsatisfied dependency expressed through field 'securePort'; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Integer'; nested exception is java.lang.NumberFormatException: For input string: "${securePort}"

Here is the config file:

@Configuration
public class ServerConfig {

  @Value("${previousScheme}")
  private String previousScheme;
  @Value("${securePort}")
  private Integer securePort;
  @Value("${previousPort}")
  private Integer previousPort;
  @Value("${setSecure}")
  private boolean setSecure;

  @Bean
  public ServletWebServerFactory servletContainer() {
    TomcatServletWebServerFactory tomcat =
        new TomcatServletWebServerFactory() {
          @Override
          protected void postProcessContext(Context context) {
            SecurityConstraint securityConstraint = new SecurityConstraint();
            securityConstraint.setUserConstraint("CONFIDENTIAL");
            SecurityCollection collection = new SecurityCollection();
            collection.addPattern("/*");
            securityConstraint.addCollection(collection);
            context.addConstraint(securityConstraint);
          }
        };
    tomcat.addAdditionalTomcatConnectors(getHttpConnector());
    return tomcat;
  }

  private Connector getHttpConnector() {
    Connector connector = new Connector(TomcatServletWebServerFactory.DEFAULT_PROTOCOL);
    connector.setScheme(previousScheme);
    connector.setPort(previousPort);
    connector.setSecure(setSecure);
    connector.setRedirectPort(securePort);
    return connector;
  }
}

yaml:

server:
  ssl:
    key-store: classpath:keystore.p12
    key-store-password: password
    key-store-type: pkcs12
    key-alias: tomcat
    key-password: password
    protocol: TLS
    enabled: true
    enabled-protocols: TLSv1.2,TLSv1.3

  port: 8443

previousPort: 8080
setSecure: false
previousScheme: "http"
securePort: 8443

These two test works fine before i added @Value

And the test:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = ServerConfig.class)
public class ServerConfigTest {

  @Autowired
  CapifServerConfig ServerConfig;

  @Test
  public void givenValidInitializationWhenServletContainerThenServletWebServerFactory() {
    assertNotNull(ServerConfig.servletContainer()); //error
  }

  @Test
  public void givenNullPortWhenServletContainerThenNull() {
    assertEquals(ServerConfig.servletContainer().getWebServer().getPort(), -1); //error
  }
}
Leo S
  • 319
  • 2
  • 16
  • Possible duplication of [this question](https://stackoverflow.com/questions/52293575/value-annotation-not-working-in-junit-test) – Erik Karlstrand Aug 04 '20 at 11:25
  • 2
    Does this answer your question? [Spring @Value always gives an error if property is Integer](https://stackoverflow.com/questions/49793547/spring-value-always-gives-an-error-if-property-is-integer) – Mladen Savić Aug 04 '20 at 11:26
  • And why shouldn't it give an exception? You only load the config not the yml containing the properties, so obviously it will fail. This isn't a Spring Boot test but rather a regular Spring test so all the added things like property loading from `applicaiton.yml` aren't done for you. – M. Deinum Aug 04 '20 at 11:59

0 Answers0