软断言时引发的异常无法通过后续测试 [英] Exceptions thrown while soft asserting fail the subsequent tests
问题描述
按照标题,我试图循环运行一个测试用例.为了能够计算失败的断言的数量,我希望如果AssertJ试图断言方法调用的返回值,它应该软失败一次迭代并继续执行.否则,它违背了软断言的目的.这是说明这一点的代码段:
As per title, I'm trying to run a test case in a loop. To be able to calculate the number of failed assertions, I'm expecting that if AssertJ is trying to assert the returned value from a method call, it should softly fail a single iteration and carry on. Otherwise, it defies the purpose of soft assertions. Here's a snippet illustrating this:
public static void main(String[] args) {
SoftAssertions softAssertions = new SoftAssertions();
softAssertions.assertThat(throwException(10)).isTrue();
softAssertions.assertThat(throwException(10)).isTrue();
softAssertions.assertThat(throwException(1)).isTrue();
softAssertions.assertAll();
}
private static boolean throwException(int stuff){
if(stuff == 1){
throw new RuntimeException();
}
return true;
}
输出:
Exception in thread "main" java.lang.RuntimeException
at eLCMUpdate.throwException(MyClass.java:101)
at eLCMUpdate.main(MyClass.java:95)
我在这里丢失了一些东西.我在做错什么吗?
I'm missing something here. Am I doing something wrong?
推荐答案
根据我的理解,软断言适用于布尔值而不适用于异常.
According to my understanding soft assertions work on boolean values and not on exceptions.
也:如果在调用softAssertions.assertAll()
之前引发异常,则显然也永远不会执行此方法.这实际上是您举报行为的原因.
Also: if you throw an exception before calling softAssertions.assertAll()
, obviously this method will also never be executed. This is actually the cause of the behaviour you reported.
只需尝试调试代码,您就会发现softAssertions.assertAll()
从未被调用.
Just try to debug through your code and you will see that the softAssertions.assertAll()
is never called.
如果将代码更改为以下内容,则软断言将正常工作:
Soft assertions will work properly if you change your code to:
@Test
void soft_assertions() {
SoftAssertions softAssertions = new SoftAssertions();
softAssertions.assertThat(checkCondition(10)).isTrue();
softAssertions.assertThat(checkCondition(10)).isTrue();
softAssertions.assertThat(checkCondition(1)).isTrue();
softAssertions.assertThat(checkCondition(2)).isTrue();
softAssertions.assertThat(checkCondition(20)).isTrue();
softAssertions.assertAll();
}
private static boolean checkCondition(int stuff){
if(stuff == 1 || stuff == 2){
return false;
}
return true;
}
这将输出多个断言的结果,并且不会停止对第一个失败的断言的求值.
This will output the result of multiple assertions and not stop on the evaluation of the first failed assertion.
org.assertj.core.api.SoftAssertionError:
The following 2 assertions failed:
1)
Expecting:
<false>
to be equal to:
<true>
but was not.
at JsonStewardshipCustomerConversionTest.soft_assertions(JsonStewardshipCustomerConversionTest.java:301)
2)
Expecting:
<false>
to be equal to:
<true>
but was not.
at JsonStewardshipCustomerConversionTest.soft_assertions(JsonStewardshipCustomerConversionTest.java:302)
更新
SoftAssertion似乎不符合您的目的.
Update
SoftAssertion does not seem to fit your purpose.
我建议您改用JUnit 5 assertAll
.根据我的测试,它会评估assertAll
块中的所有条件,并且也能幸免于异常.这里的问题是您需要尚未被广泛采用的JUnit 5.
I suggest you use instead JUnit 5 assertAll
. According to my tests it evaluates all conditions in an assertAll
block and survives exceptions too. The problem here is you need JUnit 5 which is probably not largely adopted yet.
这是一个布尔值条件失败的示例,也是一个异常.两者都在控制台中报告.
Here is an example with a failure on a boolean condition and also an exception. Both are reported in the console.
@Test
void soft_assertions() {
assertAll("Check condition",
() -> assertThat(checkCondition(9)).isTrue(),
() -> assertThat(checkCondition(10)).isTrue(),
() -> assertThat(checkCondition(11)).isTrue(),
() -> assertThat(checkCondition(2)).isTrue(), // Throws exception
() -> assertThat(checkCondition(3)).isFalse(), // fails
() -> assertThrows(IllegalArgumentException.class, () -> {
checkCondition(1);
})
);
}
private static boolean checkCondition(int stuff) {
if (stuff == 1 || stuff == 2) {
throw new IllegalArgumentException();
}
return true;
}
您将在输出中看到这一点:
You will see this in the output:
org.opentest4j.MultipleFailuresError: Check condition (2 failures)
<no message> in java.lang.IllegalArgumentException
Expecting:
<true>
to be equal to:
<false>
but was not.
这篇关于软断言时引发的异常无法通过后续测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!