【问题标题】:Read spring property source before xml resource in test在测试中读取xml资源之前的spring属性源
【发布时间】:2016-12-02 12:42:50
【问题描述】:

在 spring boot 1.4 中,在主类中我有一个导入 xml 资源的配置类。

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

src/test/resources/application.properties 中,我为

提供了该属性
datasource.database.master.pool-size=1

但我仍然面临问题,我尝试添加订单和@PropertySource 以及@TestPropertySource,但它们都没有工作。

以下是例外

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

不运行测试时它工作正常,因为属性是从 Spring Cloud 配置服务器读取的。

下面是测试用例

@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());
    }



}

【问题讨论】:

  • 添加您的测试用例。
  • @M.Deinum 测试用例已添加。

标签: java spring testing spring-boot functional-testing


【解决方案1】:

您是否尝试将 PropertySourcesPlaceholderConfigurer bean 添加到您的 java 配置中以解析 @Value 注释中的 ${...} 表达式。

通过添加这个内部类:

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

如果需要将@ContextConfiguration(loader=AnnotationConfigContextLoader.class) 添加到您的VCControllerTest 测试类以加载@Configuration 类。

【讨论】:

  • 感谢您的帮助!您的解决方案有效,但是我必须做一些更改而不是 PropertyPlaceholderConfigurer 我必须使用 PropertySourcesPlaceholderConfigurer,因为某些属性没有加载,我在文档中发现它更喜欢使用 PropertySourcesPlaceholderConfigurer。其次,它没有添加@ContextConfiguration,你为什么认为我应该添加它?
  • 提到的@ContextConfiguration 是一个开销,因为它被spring-boot 自动配置覆盖,所以..你可以丢弃它。
  • 感谢您注意到错误。 PropertyPlaceholderConfigurer 不支持 @Value 注释。我用正确的 bean 定义更新了我的答案。
【解决方案2】:

此错误通常在属性未解析时发生。

src/test/resources 下为您的测试属性添加一个resources 目录,这应该可以工作。

【讨论】:

  • 我这边打错了,application.properties的路径是src/test/resources。仍然无法正常工作:(。
  • app-config-c3p0.xml 在哪里?
  • 它在src/main/resources 中。我只想覆盖环境属性。
猜你喜欢
  • 1970-01-01
  • 2019-04-24
  • 2012-05-18
  • 2020-02-23
  • 2012-11-19
  • 1970-01-01
  • 1970-01-01
  • 2021-10-07
  • 1970-01-01
相关资源
最近更新 更多