首页 > 解决方案 > Intellij 关于 SoftAssertion 后空检查的警告

问题描述

我有一些测试,其中我创建了多个验证器函数,这些函数在每个测试用例上运行多个断言。我已经将这些验证器设置为将其实例SoftAssertions传递给每个验证器,并assertAll()在所有验证器函数运行后调用。对于我的一个验证器,我有以下断言:

TestArtifact artifactA = testArtifacts.getArtifactA();
softAsserts.assertThat(artifactA)
    .as("TestArtifactA should not be null")
    .isNotNull();

if (artifactA != null) {
    softAsserts.assertThat(artifactA.getTimestamp())
        .isEqualTo(expectedTimestamp);

    // additional assertions omitted
}

我的理解是,由于SoftAssertions只会在调用函数时测试用例失败assertAll(),所以上面的空检查是必要的,以防上面SoftAssertion的错误。但是,在 Intellij 中,我在 if 语句中收到一条警告消息,说:

Condition 'artifactA != null' is always 'true' 

我已经确认,如果isNotNull()软断言被删除,警告就会消失。这个警告只是来自 Intellij 的误报吗?或者我是否遗漏了一些SoftAssertions实际上使断言后的空检查变得不必要的东西?

标签: javaintellij-ideaassertj

解决方案


推荐阅读