0

normal use of nice assertj-matchers like isEqualByComparingTo:

BigDecimal number = ...
assertThat(number).isEqualByComparingTo(BigDecimal.valueOf(...));

however I have a list of BigDecimals and want to check each element in the list for equality by using assertj's matchers like isEqualByComparingTo:

List<BigDecimal> numbers = ...
assertThat(numbers).allMatch( ???.isEqualByComparingTo(BigDecimal.valueOf(...) )

instead i have to use the tedious low-level comparisons:

List<BigDecimal> numbers = ...
assertThat(numbers).allMatch( number -> number.compareTo(...) == 0 )

is it somehow possible, to use the nice matchers inside a predicate?

hotzen
  • 2,800
  • 1
  • 28
  • 42

1 Answers1

1

Try usingElementComparator with BigDecimalComparator (or write your own BigDecimalComparator).

Example:

List<BigDecimal> numbers = list(new BigDecimal("1.00"), new BigDecimal("2.00"));

assertThat(numbers).usingElementComparator(new BigDecimalComparator())
                   .contains(new BigDecimal("1.0"), new BigDecimal("2"));
Joel Costigliola
  • 6,308
  • 27
  • 35