在@EnabledIf 表达式中找不到变量 junitDisplayName
Variable junitDisplayName not found in @EnabledIf expression
我正在尝试在 gitlab ci runner 上拆分我的单元测试,因此我想像这样注释我的父测试类
@EnabledIf("#{systemEnvironment['CI_NODE_INDEX'] == null || junitDisplayName.hashCode() % systemEnvironment['CI_NODE_TOTAL'] == systemEnvironment['CI_NODE_INDEX']}")
但我遇到了异常
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'junitDisplayName' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public or not valid?
但是文档说应该有这样一个字段:https://junit.org/junit5/docs/5.3.0/api/org/junit/jupiter/api/condition/EnabledIf.html
我尝试了您提供的片段来搜索修复,但找不到有效的解决方案。为了避免这个问题,我设置了一个自定义 ExecutionCondition
,当显示名称包含文本“已禁用”时跳过测试 (class),并使用 @ExtendWith
将条件包含在测试 class:
DemoApplicationTests
package com.example.demo;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
@ExtendWith(CustomExecutionCondition.class)
//@DisplayName("disabled")
class DemoApplicationTests {
@Test
@DisplayName("enabled")
void shouldBeEnabled() {
}
@Test
@DisplayName("disabled")
void shouldBeDisabled() {
throw new IllegalStateException("Should not happen");
}
}
package com.example.demo;
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
import org.junit.jupiter.api.extension.ExecutionCondition;
import org.junit.jupiter.api.extension.ExtensionContext;
public class CustomExecutionCondition implements ExecutionCondition {
@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
String displayName = context.getDisplayName();
// int ciNodeIndex = Integer.parseInt(System.getProperty("CI_NODE_INDEX"));
// int ciNodeTotal = Integer.parseInt(System.getProperty("CI_NODE_TOTAL"));
if (displayName != null && displayName.contains("disabled")) {
return ConditionEvaluationResult.disabled("Test '" + displayName + "' is disabled");
}
return ConditionEvaluationResult.enabled("Test '" + displayName + "' is enabled");
}
}
我省略了系统 属性&hashcode 评估以简化示例。条件被评估三次;一次用于测试 class,两次用于测试方法。如果class级别评估returns为假,则跳过class中的所有测试方法。
我正在尝试在 gitlab ci runner 上拆分我的单元测试,因此我想像这样注释我的父测试类
@EnabledIf("#{systemEnvironment['CI_NODE_INDEX'] == null || junitDisplayName.hashCode() % systemEnvironment['CI_NODE_TOTAL'] == systemEnvironment['CI_NODE_INDEX']}")
但我遇到了异常
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E: Property or field 'junitDisplayName' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public or not valid?
但是文档说应该有这样一个字段:https://junit.org/junit5/docs/5.3.0/api/org/junit/jupiter/api/condition/EnabledIf.html
我尝试了您提供的片段来搜索修复,但找不到有效的解决方案。为了避免这个问题,我设置了一个自定义 ExecutionCondition
,当显示名称包含文本“已禁用”时跳过测试 (class),并使用 @ExtendWith
将条件包含在测试 class:
DemoApplicationTests
package com.example.demo;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
@ExtendWith(CustomExecutionCondition.class)
//@DisplayName("disabled")
class DemoApplicationTests {
@Test
@DisplayName("enabled")
void shouldBeEnabled() {
}
@Test
@DisplayName("disabled")
void shouldBeDisabled() {
throw new IllegalStateException("Should not happen");
}
}
package com.example.demo;
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
import org.junit.jupiter.api.extension.ExecutionCondition;
import org.junit.jupiter.api.extension.ExtensionContext;
public class CustomExecutionCondition implements ExecutionCondition {
@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
String displayName = context.getDisplayName();
// int ciNodeIndex = Integer.parseInt(System.getProperty("CI_NODE_INDEX"));
// int ciNodeTotal = Integer.parseInt(System.getProperty("CI_NODE_TOTAL"));
if (displayName != null && displayName.contains("disabled")) {
return ConditionEvaluationResult.disabled("Test '" + displayName + "' is disabled");
}
return ConditionEvaluationResult.enabled("Test '" + displayName + "' is enabled");
}
}
我省略了系统 属性&hashcode 评估以简化示例。条件被评估三次;一次用于测试 class,两次用于测试方法。如果class级别评估returns为假,则跳过class中的所有测试方法。