public class SoftAssertions extends Object
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();
assertThat(mansion.guests()).as("Living Guests").isEqualTo(7);
assertThat(mansion.kitchen()).as("Kitchen").isEqualTo("clean");
assertThat(mansion.library()).as("Library").isEqualTo("clean");
assertThat(mansion.revolverAmmo()).as("Revolver Ammo").isEqualTo(6);
assertThat(mansion.candlestick()).as("Candlestick").isEqualTo("pristine");
assertThat(mansion.colonel()).as("Colonel").isEqualTo("well kempt");
assertThat(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 SoftAssertions instance instead of the static methods on Assertions as follows:
@Test
public void host_dinner_party_where_nobody_dies() {
Mansion mansion = new Mansion();
mansion.hostPotentiallyMurderousDinnerParty();
SoftAssertions softly = new SoftAssertions();
softly.assertThat(mansion.guests()).as("Living Guests").isEqualTo(7);
softly.assertThat(mansion.kitchen()).as("Kitchen").isEqualTo("clean");
softly.assertThat(mansion.library()).as("Library").isEqualTo("clean");
softly.assertThat(mansion.revolverAmmo()).as("Revolver Ammo").isEqualTo(6);
softly.assertThat(mansion.candlestick()).as("Candlestick").isEqualTo("pristine");
softly.assertThat(mansion.colonel()).as("Colonel").isEqualTo("well kempt");
softly.assertThat(mansion.professor()).as("Professor").isEqualTo("well kempt");
softly.assertAll();
}
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.
SoftAssertions works by providing you with proxyies of the AssertJ assertion objects (those created by
Assertions#assertThat...) whose assertion failures are caught and stored. Only when you call
assertAll() will a SoftAssertionError be thrown containing the error messages of those
previously caught assertion failures.
Note that because SoftAssertions is stateful you should use a new instance of SoftAssertions per test method. Also, if you forget to call assertAll() at the end of your test, 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.
for the inspiration| Modifier and Type | Field and Description |
|---|---|
protected org.assertj.core.api.SoftAssertions.ErrorCollector |
collector |
| Constructor and Description |
|---|
SoftAssertions()
Creates a new
SoftAssertions. |
| Modifier and Type | Method and Description |
|---|---|
void |
assertAll()
Verifies that no proxied assertion methods have failed.
|
BigDecimalAssert |
assertThat(BigDecimal actual)
Creates a new instance of
. |
BooleanAssert |
assertThat(boolean actual)
Creates a new instance of
. |
BooleanAssert |
assertThat(Boolean actual)
Creates a new instance of
. |
BooleanArrayAssert |
assertThat(boolean[] actual)
Creates a new instance of
. |
ByteAssert |
assertThat(byte actual)
Creates a new instance of
. |
ByteAssert |
assertThat(Byte actual)
Creates a new instance of
. |
ByteArrayAssert |
assertThat(byte[] actual)
Creates a new instance of
. |
CharacterAssert |
assertThat(char actual)
Creates a new instance of
. |
CharArrayAssert |
assertThat(char[] actual)
Creates a new instance of
. |
CharacterAssert |
assertThat(Character actual)
Creates a new instance of
. |
CharSequenceAssert |
assertThat(CharSequence actual)
Creates a new instance of
. |
ClassAssert |
assertThat(Class<?> actual)
Creates a new instance of
|
DateAssert |
assertThat(Date actual)
Creates a new instance of
. |
DoubleAssert |
assertThat(double actual)
Creates a new instance of
. |
DoubleAssert |
assertThat(Double actual)
Creates a new instance of
. |
DoubleArrayAssert |
assertThat(double[] actual)
Creates a new instance of
. |
FileAssert |
assertThat(File actual)
Creates a new instance of
. |
FloatAssert |
assertThat(float actual)
Creates a new instance of
. |
FloatAssert |
assertThat(Float actual)
Creates a new instance of
. |
FloatArrayAssert |
assertThat(float[] actual)
Creates a new instance of
. |
InputStreamAssert |
assertThat(InputStream actual)
Creates a new instance of
. |
IntegerAssert |
assertThat(int actual)
Creates a new instance of
. |
IntArrayAssert |
assertThat(int[] actual)
Creates a new instance of
. |
IntegerAssert |
assertThat(Integer actual)
Creates a new instance of
. |
<T> IterableAssert<T> |
assertThat(Iterable<T> actual)
Creates a new instance of
. |
<T> IterableAssert<T> |
assertThat(Iterator<T> actual)
Creates a new instance of
. |
<T> ListAssert<T> |
assertThat(List<T> actual)
Creates a new instance of
. |
LongAssert |
assertThat(long actual)
Creates a new instance of
. |
LongAssert |
assertThat(Long actual)
Creates a new instance of
. |
LongArrayAssert |
assertThat(long[] actual)
Creates a new instance of
. |
<K,V> MapAssert<K,V> |
assertThat(Map<K,V> actual)
Creates a new instance of
. |
ShortAssert |
assertThat(short actual)
Creates a new instance of
. |
ShortAssert |
assertThat(Short actual)
Creates a new instance of
. |
ShortArrayAssert |
assertThat(short[] actual)
Creates a new instance of
. |
StringAssert |
assertThat(String actual)
Creates a new instance of
. |
<T> ObjectAssert<T> |
assertThat(T actual)
Creates a new instance of
. |
<T> ObjectArrayAssert<T> |
assertThat(T[] actual)
Creates a new instance of
. |
ThrowableAssert |
assertThat(Throwable actual)
Creates a new instance of
. |
protected <T,V> V |
proxy(Class<V> assertClass,
Class<T> actualClass,
T actual) |
public SoftAssertions()
SoftAssertions.public void assertAll()
SoftAssertionError - if any proxied assertion objects threwpublic BigDecimalAssert assertThat(BigDecimal actual)
BigDecimalAssert.actual - the actual value.public BooleanAssert assertThat(boolean actual)
BooleanAssert.actual - the actual value.public BooleanAssert assertThat(Boolean actual)
BooleanAssert.actual - the actual value.public BooleanArrayAssert assertThat(boolean[] actual)
BooleanArrayAssert.actual - the actual value.public ByteAssert assertThat(byte actual)
ByteAssert.actual - the actual value.public ByteAssert assertThat(Byte actual)
ByteAssert.actual - the actual value.public ByteArrayAssert assertThat(byte[] actual)
ByteArrayAssert.actual - the actual value.public CharacterAssert assertThat(char actual)
CharacterAssert.actual - the actual value.public CharArrayAssert assertThat(char[] actual)
CharArrayAssert.actual - the actual value.public CharacterAssert assertThat(Character actual)
CharacterAssert.actual - the actual value.public ClassAssert assertThat(Class<?> actual)
ClassAssertactual - the actual value.public <T> IterableAssert<T> assertThat(Iterable<T> actual)
IterableAssert.actual - the actual value.public <T> IterableAssert<T> assertThat(Iterator<T> actual)
actual - the actual value.public DoubleAssert assertThat(double actual)
DoubleAssert.actual - the actual value.public DoubleAssert assertThat(Double actual)
DoubleAssert.actual - the actual value.public DoubleArrayAssert assertThat(double[] actual)
DoubleArrayAssert.actual - the actual value.public FileAssert assertThat(File actual)
FileAssert.actual - the actual value.public InputStreamAssert assertThat(InputStream actual)
InputStreamAssert.actual - the actual value.public FloatAssert assertThat(float actual)
FloatAssert.actual - the actual value.public FloatAssert assertThat(Float actual)
FloatAssert.actual - the actual value.public FloatArrayAssert assertThat(float[] actual)
FloatArrayAssert.actual - the actual value.public IntegerAssert assertThat(int actual)
IntegerAssert.actual - the actual value.public IntArrayAssert assertThat(int[] actual)
IntArrayAssert.actual - the actual value.public IntegerAssert assertThat(Integer actual)
IntegerAssert.actual - the actual value.public <T> ListAssert<T> assertThat(List<T> actual)
ListAssert.actual - the actual value.public LongAssert assertThat(long actual)
LongAssert.actual - the actual value.public LongAssert assertThat(Long actual)
LongAssert.actual - the actual value.public LongArrayAssert assertThat(long[] actual)
LongArrayAssert.actual - the actual value.public <T> ObjectAssert<T> assertThat(T actual)
ObjectAssert.actual - the actual value.public <T> ObjectArrayAssert<T> assertThat(T[] actual)
ObjectArrayAssert.actual - the actual value.public <K,V> MapAssert<K,V> assertThat(Map<K,V> actual)
MapAssert.actual - the actual value.public ShortAssert assertThat(short actual)
ShortAssert.actual - the actual value.public ShortAssert assertThat(Short actual)
ShortAssert.actual - the actual value.public ShortArrayAssert assertThat(short[] actual)
ShortArrayAssert.actual - the actual value.public CharSequenceAssert assertThat(CharSequence actual)
CharSequenceAssert.actual - the actual value.public StringAssert assertThat(String actual)
StringAssert.actual - the actual value.public DateAssert assertThat(Date actual)
DateAssert.actual - the actual value.public ThrowableAssert assertThat(Throwable actual)
ThrowableAssert.actual - the actual value.Copyright © 2013 AssertJ. All Rights Reserved.