【问题标题】:Execute JUnit based on Constant value根据常量值执行 JUnit
【发布时间】:2021-02-01 17:10:54
【问题描述】:

我有这个定义了常量的类

public class Constants {
    public static final boolean TEST = true;
}

我想检查一下这个常量是否为 TRUE,如下所示:

@Test
@EnabledIf("'true' == Constants.TEST")
public void theMeasurementSampleScreenFromPickConfirm() throws InterruptedException {
   // some code execution
}

但我收到错误java.lang.NoSuchMethodError: 'java.lang.reflect.Method org.junit.platform.commons.util.ReflectionUtils.getRequiredMethod(java.lang.Class, java.lang.String, java.lang.Class[])'

你知道我怎样才能正确地实施这项检查吗?

【问题讨论】:

    标签: java junit junit4 junit5


    【解决方案1】:

    在比较布尔值时不要使用单引号。

    解决方案 1: 将值附加到注释本身中。

    @Test
    @EnabledIf("#{" + Constants.TEST + "}")
    public void theMeasurementSampleScreenFromPickConfirm() throws InterruptedException {
       // some code execution
    }
    

    解决方案 2: 使用 Spring 表达式语言,您可以通过提供类的完全限定名称来使用 Type 运算符。例如:如果Constants类在com.example包中,那么

    @Test
    @EnabledIf("#{T(com.example.Constants).TEST}")
    public void theMeasurementSampleScreenFromPickConfirm() throws InterruptedException {
           // some code execution
    }
    

    注意:这仅适用于具有 Spring Boot Starter Test(内部默认使用 JUnit)支持的 Spring 项目,因为不能支持 SpEL评估它是否是非春季项目。 JUnit 不支持在没有 Spring 的情况下独立评估 SpEL

    因此,使用 spring-boot-starter-test 创建一个 Spring Boot 项目并使用来自 spring-boot-starter-test@EnabledIf 注释,它能够评估 Spring 表达式语言。

    【讨论】:

    • 两个提议的解决方案都以org.junit.jupiter.engine.execution.ConditionEvaluationException: Failed to evaluate condition 失败。
    • 确实,这个不错
    • 很好的解决方案,但我得到了org.junit.jupiter.engine.execution.ConditionEvaluationException: Failed to evaluate condition [org.junit.jupiter.api.condition.EnabledIfCondition]: [#{T(org.automation.utils).TEST == true}] is not a valid fully qualified method name: it must start with a fully qualified class name followed by a '#' and then the method name, optionally followed by a parameter list enclosed in parentheses.
    • 再次org.junit.jupiter.engine.execution.ConditionEvaluationException: Failed to evaluate condition [org.junit.jupiter.api.condition.EnabledIfCondition]: [#{T(org.automation.utils.Constants).TEST == true}] is not a valid fully qualified method name: it must start with a fully qualified class name followed by a '#' and then the method name, optionally followed by a parameter list enclosed in parentheses.
    • 可能与变量声明有关:package org.automation.utils; public class Constants { public static final boolean TEST = true; }
    【解决方案2】:

    当使用 Spring 的 @EnabledIf 时,您可以使用 SpEL 表达式。见@EnabledIf With a SpEL Expression

    要引用常量,请使用T operator from SpEL

    您可以使用特殊的 T 运算符来指定 java.lang.Class 的实例(类型)。静态方法也可以使用这个操作符调用

    另请注意,您的 TEST 常量是布尔值,而不是字符串。

    结合以上可以使用:

    @EnabledIf("#{T(com.sandbox.Constants).TEST == true}")
    

    甚至

    @EnabledIf("#{T(com.sandbox.Constants).TEST}")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-21
      • 1970-01-01
      • 2022-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-01
      相关资源
      最近更新 更多