junit5->assertAll()

发布时间:2026/6/24 9:17:52
junit5->assertAll() 当测试中某个断言失败时JUnit 会停止执行后续断言。如果您希望确保所有断言都得到检查而不管个别断言是否失败请使用 assertAll() 对分组断言进行处理。使用分组断言即使断言失败所有断言也会执行并且错误消息会分组在一起以便更好地进行调试TestDisplayName(Validate person properties)voidvalidatePersonProperties(){PersonpersonnewPerson(John,Doe,30);assertAll(person properties,()-assertEquals(John,person.getFirstName(),First name should match),()-assertEquals(Doe,person.getLastName(),Last name should match),()-assertTrue(person.getAge()0,Age should be positive),()-assertNotNull(person.getEmail(),Email should not be null));}如果这些测试失败结果将显示所有失败项 org.opentest4j.MultipleFailuresError: person properties (2 failures) expected: John but was: null expected: null but was: testexample.com