With AssertJ the closest is :
assertThat(product).extracting("name", "available", "amount", "price")
.containsExactly("Coat", true, 12, new BigDecimal("88.0"));
But I don't like to reference field names with String
s because their validity as field names are checked only at the runtime (known issue of reflection) and that these String
s may also be incorrectly updated during refactoring operations that we perform from the IDE.
While a little more verbose, I prefer that :
assertThat(product).extracting(Product::getName, Product::getAvailable,
Product::getAmount, Product::getPrice)
.containsExactly("Coat", true, 12, new BigDecimal("88.0"));
The advantage of AssertJ over Hamcrest that you quote is that that is really fluent : so in most of cases, you need a single import : import org.assertj.core.api.Assertions;
and for collection assertions, sometimes that : org.assertj.core.groups.Tuple;
Here JUnit 5 or 4; it doesn't really matter since you will only use JUnit as a test runner for a very simple case and leave AssertJ to perform the assertions.
Or, alternatively, what is the best way of doing that in JUnit 5
universe.
JUnit 5 (as the 4th version) doesn't provide features for flexible and fluent assertions. So doing it with the best way of JUnit 5 is bound to produce much more boiler plate code : as many assertions as fields to assert or overriding equals()/hashCode()
override that you want to avoid for fair reasons.