【问题标题】:Using @EnabledIf with spring.profiles.active property in Spring Environment within tests在测试中的 Spring Environment 中使用带有 spring.profiles.active 属性的 @EnabledIf
【发布时间】:2019-09-06 10:01:55
【问题描述】:

根据文档 (https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/test/context/junit/jupiter/EnabledIf.html#expression--),您可以使用 @EnabledIf 注释 在这样的测试类或测试方法上:

@EnabledIf("${smoke.tests.enabled}")

@EnabledIf("#{systemProperties['os.name'].toLowerCase().contains('mac')}")

其中字符串是 Spring 环境中可用属性的占位符。

假设我有以下application.yml 文件:

smoke:
  tests:
    enabled: true
spring:
  profiles:
    active: test

还有下面的测试类:

@EnabledIf(value = "#{${spring.profiles.active} == 'test'}", loadContext = true)
@SpringBootTest
public class SomeClassForTests {

    @Autowired SomeType autowiredType;

    @Test
    public void someTest() {
        // test logic...
    }

当我运行测试时,我收到以下错误:

Test ignored.

org.junit.jupiter.engine.execution.ConditionEvaluationException: Failed to evaluate condition [org.springframework.test.context.junit.jupiter.EnabledIfCondition]: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'test' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public or not valid?

    at org.junit.jupiter.engine.execution.ConditionEvaluator.evaluationException(ConditionEvaluator.java:91)
    at org.junit.jupiter.engine.execution.ConditionEvaluator.evaluate(ConditionEvaluator.java:80)
    at org.junit.jupiter.engine.execution.ConditionEvaluator.lambda$evaluate$2(ConditionEvaluator.java:66)
    at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
    at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:177)
    at java.base/java.util.stream.StreamSpliterators$WrappingSpliterator.tryAdvance(StreamSpliterators.java:302)
    at java.base/java.util.stream.Streams$ConcatSpliterator.tryAdvance(Streams.java:723)
    at java.base/java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java:127)
    at java.base/java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:502)
    at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:488)
    at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
    at java.base/java.util.stream.FindOps$FindOp.evaluateSequential(FindOps.java:150)
    at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.base/java.util.stream.ReferencePipeline.findFirst(ReferencePipeline.java:543)
    at org.junit.jupiter.engine.execution.ConditionEvaluator.evaluate(ConditionEvaluator.java:68)
    at org.junit.jupiter.engine.descriptor.JupiterTestDescriptor.shouldBeSkipped(JupiterTestDescriptor.java:182)
    at org.junit.jupiter.engine.descriptor.JupiterTestDescriptor.shouldBeSkipped(JupiterTestDescriptor.java:54)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$checkWhetherSkipped$1(NodeTestTask.java:91)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.checkWhetherSkipped(NodeTestTask.java:91)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:71)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1540)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$4(NodeTestTask.java:112)
    at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:72)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:98)
    at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:74)
    at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
    at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:220)
    at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$6(DefaultLauncher.java:188)
    at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:202)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:181)
    at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:128)
    at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:74)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'test' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public or not valid?
    at org.springframework.context.expression.StandardBeanExpressionResolver.evaluate(StandardBeanExpressionResolver.java:164)
    at org.springframework.test.context.junit.jupiter.AbstractExpressionEvaluatingCondition.evaluateExpression(AbstractExpressionEvaluatingCondition.java:164)
    at org.springframework.test.context.junit.jupiter.AbstractExpressionEvaluatingCondition.evaluateAnnotation(AbstractExpressionEvaluatingCondition.java:107)
    at org.springframework.test.context.junit.jupiter.EnabledIfCondition.evaluateExecutionCondition(EnabledIfCondition.java:46)
    at org.junit.jupiter.engine.execution.ConditionEvaluator.evaluate(ConditionEvaluator.java:75)
    ... 37 more
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'test' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public or not valid?
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:217)
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:104)
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:91)
    at org.springframework.expression.spel.ast.OpEQ.getValueInternal(OpEQ.java:42)
    at org.springframework.expression.spel.ast.OpEQ.getValueInternal(OpEQ.java:32)
    at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:109)
    at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:265)
    at org.springframework.context.expression.StandardBeanExpressionResolver.evaluate(StandardBeanExpressionResolver.java:161)
    ... 41 more

我对 Spring 表达式语言不太熟悉。 我到底做错了什么?


更新 1:

使用以下两个版本的注解:

@EnabledIf(value = "${spring.profiles.active == 'test'}", loadContext = true)

@EnabledIf(value = "#{spring.profiles.active == 'test'}", loadContext = true)

导致以下错误: org.junit.jupiter.engine.execution.ConditionEvaluationException: Failed to evaluate condition [org.springframework.test.context.junit.jupiter.EnabledIfCondition]: @EnabledIf("${spring.profiles.active == 'test'}") on class SomeClassForTests must evaluate to "true" or "false", not "${spring.profiles.active == 'test'}"


更新 2:

以下内容:

@EnabledIf(value = "${spring.profiles.active} == 'test'", loadContext = true)

导致以下错误:

org.junit.jupiter.engine.execution.ConditionEvaluationException: Failed to evaluate condition [org.springframework.test.context.junit.jupiter.EnabledIfCondition]: @EnabledIf("${spring.profiles.active} == 'test'") on class SomeClassForTests must evaluate to "true" or "false", not "test == 'test'"


更新 3: 答案:

@EnabledIf(expression = "#{environment['spring.profiles.active'] == 'test'}", loadContext = true)

当您只有一个有效的个人资料时有效。如果你有很多,你会怎么做,如下所示:

smoke:
  tests:
    enabled: true
spring:
  profiles:
    active: 
    - test
    - someotherprofile

【问题讨论】:

    标签: java spring spring-test junit5


    【解决方案1】:

    尝试在 SpEL 中使用时使用额外的撇号来包装属性:

    @EnabledIf(value = "#{'${spring.profiles.active}' == 'test'}", loadContext = true)
    

    当我尝试使用自定义属性时,我为我工作(终于!)

    【讨论】:

      【解决方案2】:

      要检查 Spring Environment 中属性的确切值,您应该使用以下方法。

      @EnabledIf(expression = "#{environment['spring.profiles.active'] == 'test'}", loadContext = true)
      

      要检查哪些配置文件当前在 Spring Environment 中处于活动状态,您应该使用以下方法。

      @EnabledIf(expression = "#{environment.acceptsProfiles('test', 'someotherprofile')}", loadContext = true)
      

      【讨论】:

      • 谢谢您,当您只有一个有效的个人资料时,这很有效。但是,如果您有许多活动配置文件,您将如何使用 spring 表达式来检查您关心的配置文件是否在活动配置文件列表中? (我已经用更多细节更新了这个问题)。
      • 我已经更新了我的答案,以展示如何检查多个活动配置文件。
      • @SamBrannen 我们使用Zonky 在嵌入式 Postgres DB 上运行集成测试。这是通过在测试类上放置@AutoConfigureEmbeddedDatabase 来配置的。使用loadContext=true,Zonky 总是被初始化,即使表达式的计算结果为假。这在由于缺少进程权限(例如在容器内)而无法启动 Zonky 的情况下是有问题的,这正是我们希望基于某些活动配置文件禁用测试的地方。你能想出解决这个困境的方法吗?
      • @SamBrannen 我已经尝试在类级别上使用@EnabledIf和给定的表达式,但没有成功。无论我配置了多少或配置了什么配置文件,测试都会被执行。我试过@DisabledIf,结果相同。我没有纯 Junit5 设置,因为某些依赖项使用带有 junit-vintage-engine 的 Junit4。任何想法如何完全基于弹簧配置文件禁用测试类都会很棒!
      【解决方案3】:

      你的表达应该是这样的:

      @EnabledIf(value = "${spring.profiles.active == 'test'}", loadContext = true)
      

      【讨论】:

      • 现在错误是:org.junit.jupiter.engine.execution.ConditionEvaluationException: Failed to evaluate condition [org.springframework.test.context.junit.jupiter.EnabledIfCondition]: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'spring' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public or not valid?
      • 使用 $ 符号代替 #
      • 我在原始问题中添加了更新,其中包含更多注释及其错误消息。不幸的是,使用 $ 而不是 # 也不起作用。
      猜你喜欢
      • 2021-12-07
      • 2017-06-06
      • 2021-03-08
      • 1970-01-01
      • 2017-08-03
      • 2020-02-10
      • 2019-03-20
      • 2019-11-16
      • 2016-01-29
      相关资源
      最近更新 更多