2017-08-03 84 views
1

的價值在於它可以使用系統屬性爲@WebappConfiguration註釋的價值?我試過類似的東西:使用系統屬性作爲@WebAppConfiguration

@WebAppConfiguration("#{systemProperties['webapproot'] ?: 'war'}") 

但它似乎並沒有工作。有沒有其他方法可以通過春天來做到這一點?我不想通過我們的構建工具來做到這一點,因爲它會中斷執行我們的IDE集成測試。

+0

使用$而不是#也不起作用 – naze

回答

0

我已經找到了解決這一問題。我通過擴展WebTestContextBootstrapper來編寫自己的ContextBootsTraper,這是Spring用來加載WebAppConfiguration -Annotation的值。

我擴展功能,以包括一個檢查,如果某個系統屬性存在並覆蓋註解值,如果它的作用:

public class SystemPropTestContextBootstrapper extends WebTestContextBootstrapper { 

    @Override 
    protected MergedContextConfiguration processMergedContextConfiguration(MergedContextConfiguration mergedConfig) { 
     WebAppConfiguration webAppConfiguration = AnnotationUtils.findAnnotation(mergedConfig.getTestClass(), 
       WebAppConfiguration.class); 
     if (webAppConfiguration != null) { 
      //implementation ommited 
      String webappDir = loadWebappDirFromSystemProperty(); 
      if(webappDir == null) { 
       webappDir = webAppConfiguration.value(); 
      } 
      return new WebMergedContextConfiguration(mergedConfig, webappDir); 
     } 

     return mergedConfig; 
    } 
} 

這個類可以然後與@BootstrapWith -Annotation使用:

@RunWith(SpringJUnit4ClassRunner.class) 
@BootstrapWith(SystemPropTestContextBootstrapper.class) 
@WebAppConfiguration("standardDir") 
public class SomeTest { 

} 

該解決方案使我同時保持從我的IDE,這是偉大的運行測試的能力,從我的生成工具運行測試。

0

@WebAppConfiguration似乎並不既不支持也不是規劃環境地政司佔位符解決。

我檢查的方式如下:我試圖注入系統屬性和使用@Value決心佔位符。當我試圖注入一個不存在的屬性@Value失敗拋SpelEvaluationException: EL1008E: Property or field 'asd' cannot be found on object of type 'java.util.Properties'IllegalArgumentException: Could not resolve placeholder 'nonexistent_property' in value "${nonexistent_property}"分別同時@WebAppConfigurationvalue只是簡單地#{systemProperties.asd}${nonexistent_property} intialized簡單String的。

0

不,這是不幸的是不支持的。

提供給@WebAppConfiguration必須是作爲類級別的JavaDoc規定明確的資源路徑value

如果您希望我們考慮value屬性的動態評估,請隨時打開JIRA issue以請求此類支持。

問候,

山姆(Spring的TestContext框架的作者)