我将我的测试从 JUnit 4 迁移到 JUnit 5。一切正常,但我以前的注释的翻译:
@IfProfileValue(name = "run.import.tests", values = {"true"})
进入@EnabledIfSystemProperty(named = "run.import.tests", matches = "true")
没有按预期工作。在迁移之前,我运行了通过参数的测试-Drun.import.tests=true
只有当我通过它时,它们才会被运行。使用 Junit 5,即使带有注释 @EnabledIfSystemProperty(named = "run.import.tests", matches = "true")
即使参数 run.import.tests
也会运行测试未设置。难道我做错了什么?
最佳答案
为了使它工作,必须添加一个“相反”的注释,所以它们一起看起来像这样:
@EnabledIfSystemProperty(named = "run.import.tests", matches = "true")
@DisabledIfSystemProperty(named = "run.import.tests", matches = "(?!true)")
我已经检查过了,如果 run.import.tests
测试类被禁用属性未设置或设置为除 true
以外的任何其他值;如果该值设置为 true
- 测试类没有被禁用。奇怪的是,the documentation of @EnabledIfSystemProperty状态:
If the specified system property is undefined, the annotated class or method will be disabled.
然而它不能那样工作,它可能是一个错误。我将尝试调试 JUnit 类,如果我在他们的 GitHub 上创建了一个问题,我会将它链接到这里。
我已经浏览了代码并对其进行了多次测试 - 这是摘要:
mvn test
) 运行测试时,注释 @EnabledIfSystemProperty
单独工作正常 - 仅当我添加 -Drun.import.tests=true
时才运行测试争论。 @DisabledIfSystemProperty
在这种情况下不需要。 Run XxxTest
运行测试时只有当两个注释都存在时,属性的处理才能正常工作。经过一些调试,我遇到了 JupiterTestEngine
- 由外部启动器(Maven、IntelliJ 等)运行的类。 IntelliJ 似乎为其测试启动器添加了一个属性:junit.jupiter.conditions.deactivate ,这通常很有用 - 多亏了这一点,我们甚至可以运行在本地使用条件注释禁用的测试,而忽略它们。该属性的值为 org.junit.*Enabled*Condition
当@DisabledIfSystemProperty
不存在且 org.junit.*Disabled*Condition
当它是 - 条件是 JUnit 的扩展,用于解决测试的禁用状态。 (2) 中描述的功能通常很有用,但在您的情况下,它看起来像是注释不起作用。它实际上是有效的,但 IntelliJ 只是绕过了它。
关于java - JUnit 5 @EnabledIfSystemProperty 无法按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63250350/