Spring-boot: java.lang.NumberFormatException: For input string: "${local.server.port}"

Created on 24 Dec 2014  路  7Comments  路  Source: spring-projects/spring-boot

I want to run SomeSystemTest and use the IntegrationTest to run a Spring-Application right before I use Selenium to test the UI (Some guy on stackoverflow suggested this approach: http://stackoverflow.com/questions/27625995/start-and-control-spring-application-with-junit). But I always get the following Exception:

Could not autowire field: int SystemTest.port; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'int'; nested exception is java.lang.NumberFormatException: For input string: "${local.server.port}"

I found a few postings regarding this exception and I tried a lot, but this trial and error approach was not successful at all.

I want to

  • configure the testing environment with a single config file
  • start a local server (maybe on a random port)
  • trigger execution of testcases

What I did:

  • created a application-testing.properties file within my src/main/resources
  • created the following files

FILES:

#application-testing.properties
server.port=8001
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
@ContextConfiguration(classes = {SystemTestConfig.class})
@IntegrationTest
@ActiveProfiles("testing")
public abstract class SystemTest {

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

    @Autowired
    protected WebDriver driver;

    protected String getBaseUrl(){
        return "http://localhost:" + port;
    }
}
//TestConfig is an empty Configuration class
@Configuration
@Profile("testing")
public class SystemTestConfig extends TestConfig{
    @Bean
    public WebDriver getWebdriver(){
        return new HtmlUnitDriver();
    }
}
public class SomeSystemTest extends SystemTest{

    @Test
    public void testSomething() {
        driver.get("http://localhost:8080/user/login");
        assertEquals(driver.getCurrentUrl().toString(),"http://localhost:8080/user/login");
    }
}

Most helpful comment

I think the use of @ContextConfiguration on your test is causing the problem. I think it is stopping the SpringApplicationContextLoader (from @SpringApplicationConfiguration) from running, which means that the local.server.port property is not set.

Try using the the classes attribute of your @SpringApplicationConfiguration instead:

@SpringApplicationConfiguration(classes = {SampleTomcatApplication.class, SystemTestConfig.class})

All 7 comments

Can you remove the @ContextConfiguration? It probably conflicts with Boot's @SpringApplicationConfiguration

The result has not changed, it still throws the exception.

show your Application please. If you could share a project that reproduces the issue it would even be better because I am bit puzzled by the exception.

@EnableAutoConfiguration
@ComponentScan
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class);
    }
}
@Configuration
@EnableSpringDataWebSupport
@EnableJpaRepositories
@EnableTransactionManagement
public class ApplicationConfig {
}

I think the use of @ContextConfiguration on your test is causing the problem. I think it is stopping the SpringApplicationContextLoader (from @SpringApplicationConfiguration) from running, which means that the local.server.port property is not set.

Try using the the classes attribute of your @SpringApplicationConfiguration instead:

@SpringApplicationConfiguration(classes = {SampleTomcatApplication.class, SystemTestConfig.class})

thx @philwebb , now it works like a charm!

That's actually what I asked you to do in my earlier comment, sorry for the confusion.

Was this page helpful?
0 / 5 - 0 ratings