2

In spring boot 1.4, in the main class I've a configuration class which imports xml resource.

@ImportResource("classpath:app-config-c3p0.xml")

In the src/test/resources/application.properties I have provided the property for

datasource.database.master.pool-size=1

But I'm still facing issue, I tried to add order and @PropertySource also @TestPropertySource but none of them worked.

Following is the exception

Caused by: org.springframework.beans.TypeMismatchException: Failed to convert property value of type [java.lang.String] to required type [int] for property 'maxPoolSize'; nested exception is java.lang.NumberFormatException: For input string: "${datasource.database.master.pool-size}"
    at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:596)
    at org.springframework.beans.AbstractNestablePropertyAccessor.convertForProperty(AbstractNestablePropertyAccessor.java:603)
    at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:216)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1532)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1491)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1231)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
    ... 54 common frames omitted
Caused by: java.lang.NumberFormatException: For input string: "${datasource.database.master.pool-size}"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:569)
    at java.lang.Integer.valueOf(Integer.java:766)
    at org.springframework.util.NumberUtils.parseNumber(NumberUtils.java:208)
    at org.springframework.beans.propertyeditors.CustomNumberEditor.setAsText(CustomNumberEditor.java:113)
    at org.springframework.beans.TypeConverterDelegate.doConvertTextValue(TypeConverterDelegate.java:468)
    at org.springframework.beans.TypeConverterDelegate.doConvertValue(TypeConverterDelegate.java:441)
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:199)
    at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:576)
    ... 60 common frames omitted

It's working fine when not running tests as the properties are read from spring cloud config server.

Following is the test case

@WebMvcTest(VCController.class)
@RunWith(SpringRunner.class)
@TestPropertySource("classpath:test.properties")
public class VCControllerTest {

    @MockBean
    private VCGateway vCGateway;

    @Autowired
    private MockMvc mvc;

    @Test
    public void testCreateVoucher() throws Exception {
        int timeout = 10;
        CreateVC createVC = new CreateVC(timeout);
        CreatedVCModel createdVCModel = new CreatedVCModel();

        given(vCGateway.create(createVC)).willReturn(createdVCModel);


        mvc.perform(post("/v1/vc")
                .content(json(createVC))
                .accept(MediaType.APPLICATION_JSON))
                .andExpect(status().isCreated());
    }



}
2
  • Add your testcase.
    – M. Deinum
    Dec 2, 2016 at 19:57
  • @M.Deinum Testcase added.
    – Ahmed
    Dec 5, 2016 at 11:30

2 Answers 2

3
+50

Did you try adding a PropertySourcesPlaceholderConfigurer bean to your java config for resolving your ${...} expression in the @Value annotation.

By adding this inner class:

@Configuration
public static class TestContext{
        @Bean
        public static PropertySourcesPlaceholderConfigurer properties(){
          return new PropertySourcesPlaceholderConfigurer();
        }
}

If needed add @ContextConfiguration(loader=AnnotationConfigContextLoader.class) to your VCControllerTest test class for loading the @Configuration class.

3
  • Thanks for the help! Your solutions worked, but there a few changes i had to do instead of PropertyPlaceholderConfigurer i've to use PropertySourcesPlaceholderConfigurer because some of the properties were not loading and I found in docs that its preferred to use PropertySourcesPlaceholderConfigurer. Secondly it worked without adding the @ContextConfiguration, why do you think I should add it?
    – Ahmed
    Dec 7, 2016 at 5:25
  • The @ContextConfiguration mentioned is an overhead as it is overridden by the spring-boot auto configuration, so..you can discard it. Dec 7, 2016 at 8:46
  • Thx for noticing the mistake. PropertyPlaceholderConfigurer does not support the @Value annotation. I updated my answer with the correct bean definition. Dec 7, 2016 at 9:07
0

This error normally occurs when the property is not resolved.

Add a resources directory under src/test/resources for your test properties and that should work.

3
  • Typo on my side, the path of application.properties is src/test/resources. Still its not working :(.
    – Ahmed
    Dec 2, 2016 at 17:07
  • where is the app-config-c3p0.xml located?
    – dimitrisli
    Dec 2, 2016 at 17:29
  • Its in the src/main/resources. I just want to override the env properties.
    – Ahmed
    Dec 2, 2016 at 17:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.