-
-
Notifications
You must be signed in to change notification settings - Fork 765
Description
Feature summary
A method isInFloatingPointFormat to assert that a BigDecimal has been specified in floating point syntax using a decimal dot or the exponential "e".
A complementary method isInIntegerFormat asserting that the BigDecimal has been specified in an integer format should then also be added.
I first used .scale().isNotZero() as workaround, but the failure message is confusing because it refers to the scale, not the checked number. Adding withFailMessage isn't that useful as it doesn't allow printing the checked number.
Now I'm using .is(new Condition<>(bd -> bd.scale() != 0, "in floating point format")) and that works, but I think it makes sense to add a high-level assertion method that abstracts away the low-level check.
I have initially requested this for JsonUnit (lukas-krecan/JsonUnit#789), but it's more intuitive if the methods are added to AssertJ directly.
Example
import static org.assertj.core.api.Assertions.assertThat;
import java.math.BigDecimal;
import java.util.stream.Stream;
var bigDecimals = Stream.of("653.401", "12.0", "10e3", "1.2e12").map(BigDecimal::new).toList();
// These should pass
assertThat(bigDecimals).allSatisfy(bd -> assertThat(bd).isInFloatingPointFormat());
assertThat(new BigDecimal("12")).isInIntegerFormat();
// These should fail
assertThat(bigDecimals).allSatisfy(bd -> assertThat(bd).isInIntegerFormat());
assertThat(new BigDecimal("12")).isInFloatingPointFormat();