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