根据文档(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 Environment 中可用属性的占位符。
假设我有以下application.yml文件:
smoke:
tests:
enabled: true
spring:
profiles:
active: test
Run Code Online (Sandbox Code Playgroud)
以及以下测试类:
@EnabledIf(value = "#{${spring.profiles.active} == 'test'}", loadContext = true)
@SpringBootTest
public class SomeClassForTests {
@Autowired SomeType autowiredType;
@Test
public void someTest() {
// test logic...
}
Run Code Online (Sandbox Code Playgroud)
当我运行测试时,我收到以下错误:
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 …Run Code Online (Sandbox Code Playgroud)