Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AssertJ fails to assert BigDecimal equality without scale

I am running assertions like so:

assertThat(obj.getTotal()).isEqualTo(BigDecimal.valueOf(4))

I am getting

Expecting: <4.00> to be equal to: <4>

so then I tried

assertThat(obj.getTotal()).isEqualTo(BigDecimal.valueOf(4.00))

Expecting: <4.00> to be equal to: <4.0>

I found a workaround in which I would set the scale of expected value 4 to 4.00, but it seems quite irritating that I have to do so for all BigDecimal variables in my tests. Is there a better way from AssertJ that I am unaware of?

like image 381
夢のの夢 Avatar asked Dec 02 '25 02:12

夢のの夢


2 Answers

You can use isEqualByComparingTo or usingComparator that use BigDecimal.compareTo which compares the actual numeric value. The equals method which is the default assertj uses does the following according to JavaDoc:

Unlike compareTo, this method considers two BigDecimal objects equal only if they are equal in value and scale (thus 2.0 is not equal to 2.00 when compared by this method).

So you can do:

    assertThat(obj.getTotal())
        .usingComparator(BigDecimal::compareTo)
        .isEqualTo(BigDecimal.valueOf(4.00));

or simply:

assertThat(obj.getTotal())
    .isEqualByComparingTo(BigDecimal.valueOf(4.00));

To use with Iterable and extracting assertions:

    assertThat(asList(new Trade("1", new BigDecimal("5.200")), new Trade("2", new BigDecimal("4.00"))))
        .extracting(Trade::getId, Trade::getAmount)
        .usingComparatorForType(BigDecimal::compareTo, BigDecimal.class)
        .usingRecursiveFieldByFieldElementComparator()
        .containsExactlyInAnyOrder(tuple("1", new BigDecimal("5.2")), tuple("2", new BigDecimal("4")));
like image 106
Marimuthu Madasamy Avatar answered Dec 03 '25 15:12

Marimuthu Madasamy


In case you are comparing elements in collections, since AssertJ 3.20.0, you must use usingRecursiveFieldByFieldElementComparator(RecursiveComparisonConfiguration):

import org.assertj.core.api.recursive.comparison.RecursiveComparisonConfiguration;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;

import static java.util.Collections.singleton;
import static org.assertj.core.api.Assertions.assertThat;

public class BigDecimalTests {

static class Item {
    final BigDecimal length;

    public Item(BigDecimal length) {
        this.length = length;
    }

    static Item of(String length) {
        return new Item(new BigDecimal(length));
    }
}

@Test
void fail_1() {
    assertThat(singleton(Item.of("1.0")))
            .usingComparatorForType(BigDecimal::compareTo, BigDecimal.class)
            .containsExactly(Item.of("1"));
}

@Test
void fail_2() { // OK with AssertJ < 3.20.0
    assertThat(singleton(Item.of("1.0")))
            .usingComparatorForType(BigDecimal::compareTo, BigDecimal.class)
            .usingRecursiveFieldByFieldElementComparator()
            .containsExactly(Item.of("1"));
}

@Test
void fail_3() { // OK with AssertJ < 3.20.0
    assertThat(singleton(Item.of("1.0")))
            .usingRecursiveFieldByFieldElementComparator()
            .usingComparatorForType(BigDecimal::compareTo, BigDecimal.class)
            .containsExactly(Item.of("1"));
}

@Test
void ok() {
    assertThat(singleton(Item.of("1.0")))
            .usingRecursiveFieldByFieldElementComparator(
                    RecursiveComparisonConfiguration.builder()
                            .withComparatorForType(BigDecimal::compareTo, BigDecimal.class)
                            .build())
            .containsExactly(Item.of("1"));
}
}
like image 32
laurent Avatar answered Dec 03 '25 17:12

laurent