Skip to content

Add assertion methods for floating point and integer format of BigDecimal #3532

@kiwi-oss

Description

@kiwi-oss

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();

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions