在 JUnit > 5.6.0 中有什么方法可以在 class 执行条件评估之前加载上下文?
Is there any way in JUnit > 5.6.0 to load context before class execution condition evaluation?
我的目标是仅当 application-foo.properties
中声明的某些 spring 属性 等于某个常量时,才在测试 class 中执行测试。
我试图通过使用注释编写自己的 ExecutionCondition 来实现这一点,当我在 @Test
方法上使用它时它工作正常。但是在测试 class 中使用一个会导致问题:条件在 Spring 的上下文启动之前评估。
根据文档:
If an ExecutionCondition disables a test method, that does not prevent
the test class from being instantiated.
但是我想怎么办呢?
Script-based condition APIs and their supporting implementations are
deprecated with the intent to remove them in JUnit Jupiter 5.6. Users
should instead rely on a combination of other built-in conditions or
create and use a custom implementation of ExecutionCondition to
evaluate the same conditions.
从 v5.6.0 开始,@EnabledIf 注释被排除。这个的优点之一是 loadContext()
属性,它可以从字面上实现本主题的目标。
None 个剩余注释(例如 EnabledIfEnvironmentVariable) contain even a tiny hint, how to reach it. And I haven't found any possibilities in the User Guide。
我将不胜感激任何帮助或建议。
确实 import org.junit.jupiter.api.condition.EnabledIf
中的 @EnabledIf
在 JUnit 5.6 中被删除了。
还有 Spring 测试 import org.springframework.test.context.junit.jupiter.EnabledIf
中的 @EnabledIf
未弃用并且适合您的用例。
@EnabledIf(
expression = "#{systemProperties['your.property'].toLowerCase().contains('disabled')}",
reason = "Disabled due to property",
loadContext = true
)
@SpringBootTest
public class MyTest {
// your tests
}
假设您在 application-foo.properties
中有一个 属性:
flaky.tests.enabled = false
您现在可以有条件地 运行 使用
进行测试
@EnabledIf(
expression = "${flaky.tests.enabled}",
reason = "Disabled flaky tests",
loadContext = true
)
@SpringBootTest
@ActiveProfiles("foo")
public class FlakyTest {
// your tests
}
我用 Spring Boot 2.3.0 测试了它,它工作正常,没有任何弃用警告。
我的目标是仅当 application-foo.properties
中声明的某些 spring 属性 等于某个常量时,才在测试 class 中执行测试。
我试图通过使用注释编写自己的 ExecutionCondition 来实现这一点,当我在 @Test
方法上使用它时它工作正常。但是在测试 class 中使用一个会导致问题:条件在 Spring 的上下文启动之前评估。
根据文档:
If an ExecutionCondition disables a test method, that does not prevent the test class from being instantiated.
但是我想怎么办呢?
Script-based condition APIs and their supporting implementations are deprecated with the intent to remove them in JUnit Jupiter 5.6. Users should instead rely on a combination of other built-in conditions or create and use a custom implementation of ExecutionCondition to evaluate the same conditions.
从 v5.6.0 开始,@EnabledIf 注释被排除。这个的优点之一是 loadContext()
属性,它可以从字面上实现本主题的目标。
None 个剩余注释(例如 EnabledIfEnvironmentVariable) contain even a tiny hint, how to reach it. And I haven't found any possibilities in the User Guide。
我将不胜感激任何帮助或建议。
确实 import org.junit.jupiter.api.condition.EnabledIf
中的 @EnabledIf
在 JUnit 5.6 中被删除了。
还有 Spring 测试 import org.springframework.test.context.junit.jupiter.EnabledIf
中的 @EnabledIf
未弃用并且适合您的用例。
@EnabledIf(
expression = "#{systemProperties['your.property'].toLowerCase().contains('disabled')}",
reason = "Disabled due to property",
loadContext = true
)
@SpringBootTest
public class MyTest {
// your tests
}
假设您在 application-foo.properties
中有一个 属性:
flaky.tests.enabled = false
您现在可以有条件地 运行 使用
进行测试@EnabledIf(
expression = "${flaky.tests.enabled}",
reason = "Disabled flaky tests",
loadContext = true
)
@SpringBootTest
@ActiveProfiles("foo")
public class FlakyTest {
// your tests
}
我用 Spring Boot 2.3.0 测试了它,它工作正常,没有任何弃用警告。