2017-08-03 85 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框架的作者)