2016-12-02 69 views
2

在spring boot 1.4中,在主類中我有一個導入xml資源的配置類。在測試xml資源之前閱讀spring屬性源

@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 

它的正常工作的性質從春季雲配置服務器讀取未運行測試時。

以下是測試案例

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



} 
+0

添加您的測試用例。 –

+0

@ M.Deinum Testcase添加。 – Ahmed

回答

2

是否嘗試在您的java配置文件中添加PropertySourcesPlaceholderConfigurer bean以解析@Value註釋中的$ {...}表達式。

通過添加該內部類:

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

如果需要添加@ContextConfiguration(裝載機= AnnotationConfigContextLoader.class)到您的VCControllerTest測試類加載@Configuration類。

+0

感謝您的幫助! ,但是我必須做一些改變而不是'PropertyPlaceholderConfigurer'我必須使用'PropertySourcesPlaceholderConfigurer',因爲一些屬性沒有加載,我在文檔中發現它首選使用'PropertySourcesPlaceholderConfigurer'。其次,它沒有添加'@ContextConfiguration',爲什麼你認爲我應該添加它? – Ahmed

+0

提到的@ContextConfiguration是一個開銷,因爲它被彈簧引導自動配置覆蓋,所以..你可以放棄它。 –

+0

好的,所以我已經刪除它 – Ahmed

0

正常時該屬性沒有得到解決,就會出現此錯誤。

添加resources目錄下的src/test/resources爲您的測試屬性,應該工作。

+0

Typo在我身邊,application.properties的路徑是'src/test/resources'。仍然不工作:(。 – Ahmed

+0

其中是app-config-c3p0.xml位於? – dimitrisli

+0

它在'src/main/resources'。我只想覆蓋env屬性。 – Ahmed