admin管理员组

文章数量:1123085

JUnit 5 introduced lambdas for messages of failed assertions. So the message is only evaluated after a failed assertion.

For example, if I have this in TestNG:

Optional<Data> data = ...;
assertTrue(data.isEmpty(), data.get().toString());

it will cause an exception if data is isEmpty, although the test should pass. In JUnit 5 I can use this instead:

Optional<Data> data = ...;
assertTrue(data.isEmpty(), () -> data.get().toString());

So data.get().toString() is only run if it has data. No exception happens, the unit-test runs normal.

I know I can mix TestNG and JUnit, but I would prefer not to do this, because it can become confusing (for example, the parameter order for many identical named assertions is switched).

Is there any possibility to do this in TestNG, maybe with a another library, which extends TestNG?

本文标签: junit5How to use lambdas for TestNG failed assertion messagesStack Overflow