Class AutoCloseableBDDSoftAssertions
- java.lang.Object
-
- org.assertj.core.api.AbstractSoftAssertions
-
- org.assertj.core.api.Java6AbstractBDDSoftAssertions
-
- org.assertj.core.api.AbstractBDDSoftAssertions
-
- org.assertj.core.api.BDDSoftAssertions
-
- org.assertj.core.api.AutoCloseableBDDSoftAssertions
-
- All Implemented Interfaces:
AutoCloseable
,InstanceOfAssertFactories
public class AutoCloseableBDDSoftAssertions extends BDDSoftAssertions implements AutoCloseable
A version ofBDDSoftAssertions
that uses try-with-resources statement to automatically callBDDSoftAssertions.assertAll()
so that you don't forget to.Suppose we have a test case and in it we'd like to make numerous assertions. In this case, we're hosting a dinner party and we want to ensure not only that all our guests survive but also that nothing in the mansion has been unduly disturbed:
@Test public void host_dinner_party_where_nobody_dies() { Mansion mansion = new Mansion(); mansion.hostPotentiallyMurderousDinnerParty(); then(mansion.guests()).as("Living Guests").isEqualTo(7); then(mansion.kitchen()).as("Kitchen").isEqualTo("clean"); then(mansion.library()).as("Library").isEqualTo("clean"); then(mansion.revolverAmmo()).as("Revolver Ammo").isEqualTo(6); then(mansion.candlestick()).as("Candlestick").isEqualTo("pristine"); then(mansion.colonel()).as("Colonel").isEqualTo("well kempt"); then(mansion.professor()).as("Professor").isEqualTo("well kempt"); }
After running the test, JUnit provides us with the following exception message:
org.junit.ComparisonFailure: [Living Guests] expected:<[7]> but was:<[6]>
Oh no! A guest has been murdered! But where, how, and by whom?
Unfortunately frameworks like JUnit halt the test upon the first failed assertion. Therefore, to collect more evidence, we'll have to rerun the test (perhaps after attaching a debugger or modifying the test to skip past the first assertion). Given that hosting dinner parties takes a long time, this seems rather inefficient.
Instead let's change the test so that at its completion we get the result of all assertions at once. We can do that by using a AutoCloseableBDDSoftAssertions instance instead of the static methods on
BDDAssertions
as follows:@Test public void host_dinner_party_where_nobody_dies() { Mansion mansion = new Mansion(); mansion.hostPotentiallyMurderousDinnerParty(); try (AutoCloseableBDDSoftAssertions softly = new AutoCloseableBDDSoftAssertions()) { softly.then(mansion.guests()).as("Living Guests").isEqualTo(7); softly.then(mansion.kitchen()).as("Kitchen").isEqualTo("clean"); softly.then(mansion.library()).as("Library").isEqualTo("clean"); softly.then(mansion.revolverAmmo()).as("Revolver Ammo").isEqualTo(6); softly.then(mansion.candlestick()).as("Candlestick").isEqualTo("pristine"); softly.then(mansion.colonel()).as("Colonel").isEqualTo("well kempt"); softly.then(mansion.professor()).as("Professor").isEqualTo("well kempt"); // no need to call assertAll, it is done when softly is closed. } }
Now upon running the test our JUnit exception message is far more detailed:
org.assertj.core.api.SoftAssertionError: The following 4 assertions failed: 1) [Living Guests] expected:<[7]> but was:<[6]> 2) [Library] expected:<'[clean]'> but was:<'[messy]'> 3) [Candlestick] expected:<'[pristine]'> but was:<'[bent]'> 4) [Professor] expected:<'[well kempt]'> but was:<'[bloodied and disheveled]'>
Aha! It appears that perhaps the Professor used the candlestick to perform the nefarious deed in the library. We should let the police take it from here.
AutoCloseableBDDSoftAssertions works by providing you with proxies of the AssertJ assertion objects (those created by
BDDAssertions
#then...) whose assertion failures are caught and stored. Only when you callBDDSoftAssertions.assertAll()
will aSoftAssertionError
be thrown containing the error messages of those previously caught assertion failures.Note that because AutoCloseableBDDSoftAssertions is stateful you should use a new instance of AutoCloseableBDDSoftAssertions per test method. Also, if you forget to use try-with-resources statement, the test will pass even if any assertion objects threw exceptions (because they're proxied, remember?). So don't forget.
It is recommended to use
AbstractAssert.as(String, Object...)
so that the multiple failed assertions can be easily distinguished from one another.- Author:
- Brian Laframboise
- See Also:
- Reinventing Assertions (inspired this feature)
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class org.assertj.core.api.AbstractSoftAssertions
AbstractSoftAssertions.ThrowingRunnable
-
-
Field Summary
-
Fields inherited from class org.assertj.core.api.AbstractSoftAssertions
proxies
-
Fields inherited from interface org.assertj.core.api.InstanceOfAssertFactories
ARRAY, ATOMIC_BOOLEAN, ATOMIC_INTEGER, ATOMIC_INTEGER_ARRAY, ATOMIC_INTEGER_FIELD_UPDATER, ATOMIC_LONG, ATOMIC_LONG_ARRAY, ATOMIC_LONG_FIELD_UPDATER, ATOMIC_MARKABLE_REFERENCE, ATOMIC_REFERENCE, ATOMIC_REFERENCE_ARRAY, ATOMIC_REFERENCE_FIELD_UPDATER, ATOMIC_STAMPED_REFERENCE, BIG_DECIMAL, BIG_INTEGER, BOOLEAN, BOOLEAN_ARRAY, BYTE, BYTE_ARRAY, CHAR_ARRAY, CHAR_SEQUENCE, CHARACTER, CLASS, COMPLETABLE_FUTURE, COMPLETION_STAGE, DATE, DOUBLE, DOUBLE_ARRAY, DOUBLE_PREDICATE, DOUBLE_STREAM, FILE, FLOAT, FLOAT_ARRAY, FUTURE, INPUT_STREAM, INSTANT, INT_ARRAY, INT_PREDICATE, INT_STREAM, INTEGER, ITERABLE, ITERATOR, LIST, LOCAL_DATE, LOCAL_DATE_TIME, LOCAL_TIME, LONG, LONG_ARRAY, LONG_PREDICATE, LONG_STREAM, MAP, OFFSET_DATE_TIME, OFFSET_TIME, OPTIONAL, OPTIONAL_DOUBLE, OPTIONAL_INT, OPTIONAL_LONG, PATH, PREDICATE, SHORT, SHORT_ARRAY, SPLITERATOR, STREAM, STRING, STRING_BUFFER, STRING_BUILDER, THROWABLE, URI_TYPE, URL_TYPE, ZONED_DATE_TIME
-
-
Constructor Summary
Constructors Constructor Description AutoCloseableBDDSoftAssertions()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
-
Methods inherited from class org.assertj.core.api.BDDSoftAssertions
assertAll
-
Methods inherited from class org.assertj.core.api.AbstractBDDSoftAssertions
then, then, then, then, then, then, then, then, then, then, then, then, then, then, then, then, then, then, then, then, then, then, then
-
Methods inherited from class org.assertj.core.api.Java6AbstractBDDSoftAssertions
then, then, then, then, then, then, then, then, then, then, then, then, then, then, then, then, then, then, then, then, then, then, then, then, then, then, then, then, then, then, then, then, then, then, then, then, then, then, then, then, then, then, then, then, then, then, then, then, then, then, then, then, then, then, then, then, thenCode, thenObject, thenThrownBy, thenThrownBy
-
Methods inherited from class org.assertj.core.api.AbstractSoftAssertions
check, decorateErrorsCollected, errorsCollected, fail, fail, fail, failBecauseExceptionWasNotThrown, proxy, shouldHaveThrown, wasSuccess
-
-
-
-
Method Detail
-
close
public void close() throws SoftAssertionError
- Specified by:
close
in interfaceAutoCloseable
- Throws:
SoftAssertionError
-
-