java assertAll 与 JUnit5 中的多个断言
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40796756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
assertAll vs multiple assertions in JUnit5
提问by Wilhelm Olejnik
Is there any reason to group multiple assertions:
是否有任何理由将多个断言分组:
public void shouldTellIfPrime(){
Assertions.assertAll(
() -> assertTrue(isPrime(2)),
() -> assertFalse(isPrime(4))
);
}
instead of doing this:
而不是这样做:
public void shouldTellIfPrime(){
Assertions.assertTrue(isPrime(2));
Assertions.assertFalse(isPrime(4));
}
回答by Nicolai
The interesting thing about assertAll
is that it always checks all of the assertions that are passed to it, no matter how many fail. If all pass, all is fine - if at least one fails you get a detailed result of all that went wrong (and right for that matter).
有趣的assertAll
是它总是检查传递给它的所有断言,无论有多少失败。如果一切都通过,一切都很好——如果至少有一个失败,你会得到所有出错的详细结果(就这点而言是正确的)。
It is best used for asserting a set of properties that belong together conceptionally. Something where your first instinct would be, "I want to assert this as one".
它最适合用于断言在概念上属于一起的一组属性。你的第一直觉是,“我想断言这是一个”。
Example
例子
Your specific example is not an optimal use case for assertAll
because checking isPrime
with a prime and a non-prime is independent of each other - so much so that I would recommend writing two test methods for that.
您的具体示例不是最佳用例,assertAll
因为检查isPrime
素数和非素数是相互独立的 - 以至于我建议为此编写两种测试方法。
But assume you have a simple class like an address with fields city
, street
, number
and would like to assert that those are what you expect them to be:
但是,假设你有一个简单的类像场的地址city
,street
,number
并愿断言,那些是你所期望的他们是:
Address address = unitUnderTest.methodUnderTest();
assertEquals("Redwood Shores", address.getCity());
assertEquals("Oracle Parkway", address.getStreet());
assertEquals("500", address.getNumber());
Now, as soon as the first assertion fails, you will never see the results of the second, which can be quite annoying. There are many ways around this and JUnit Jupiter's assertAll
is one of them:
现在,一旦第一个断言失败,您将永远不会看到第二个断言的结果,这可能很烦人。有很多方法可以解决这个问题,JUnit JupiterassertAll
就是其中之一:
Address address = unitUnderTest.methodUnderTest();
assertAll("Should return address of Oracle's headquarter",
() -> assertEquals("Redwood Shores", address.getCity()),
() -> assertEquals("Oracle Parkway", address.getStreet()),
() -> assertEquals("500", address.getNumber())
);
If the method under test returns the wrong address, this is the error you get:
如果被测方法返回错误的地址,这是您得到的错误:
org.opentest4j.MultipleFailuresError:
Should return address of Oracle's headquarter (3 failures)
expected: <Redwood Shores> but was: <Walldorf>
expected: <Oracle Parkway> but was: <Dietmar-Hopp-Allee>
expected: <500> but was: <16>
回答by Nkosi
According to documentation here
根据此处的文档
Asserts that all supplied executables do not throw an AssertionError.
If any supplied Executable throws an AssertionError, all remaining executables will still be executed, and all failures will be aggregated and reported in a MultipleFailuresError. However, if an executable throws an exception that is not an AssertionError, execution will halt immediately, and the exception will be rethrown as is but masked as an unchecked exception.
断言所有提供的可执行文件都不会抛出 AssertionError。
如果任何提供的 Executable 抛出 AssertionError,则所有剩余的可执行文件仍将被执行,并且所有失败都将在 MultipleFailuresError 中汇总和报告。但是,如果一个可执行文件抛出一个不是 AssertionError 的异常,执行将立即停止,并且异常将按原样重新抛出,但被屏蔽为未经检查的异常。
So main difference is that the assertAllwill allow all the asserts to execute without breaking the flow while the others like assertTrueand the lot will stop the test with the AssertionError
所以主要区别在于assertAll将允许所有断言在不中断流程的情况下执行,而其他断言如assertTrue和批次将使用AssertionError停止测试
So in your first example both assertions will execute regardless of pass to fail, while in the second example test will stop if first assertion fails.
因此,在您的第一个示例中,无论通过失败,两个断言都将执行,而在第二个示例中,如果第一个断言失败,则测试将停止。
Is there any reason to group multiple assertions
是否有任何理由将多个断言分组
If you want all assertions exercised in the unit test.
如果您希望在单元测试中执行所有断言。