0

I'm a bit fan of Google's Truth.dev library. I have a large domain model in Java, and want to add several little custom assertions for them in my own Subject files. It's a bit of pain though to create the boiler plate for the Subjects every time though, and there are lot's obvious / straight forward assertions that I'd like to have by default.

Something similar to AssertJ's generator project.

For example, given the following simple model (mine is much more complicated):

@lombok.Value
public class Car {
    String name;
    Make make;
    int colourId;

    public enum Make {PLASTIC, METAL}
}

I would like to be able to do the following without coding anything myself:

assertThat(car).hasMakeEqualTo(PLASTIC);
assertThat(car).hasColourId().isAtLeast(5);
assertThat(car).hasName().ignoringTrailingWhiteSpace().equalTo("sally");

or

assertThat(person).hasAddress().hasStreet().ignoringCase().endsWith('Rd')
Antony Stubbs
  • 13,161
  • 5
  • 35
  • 39

1 Answers1

0

Upon my discovery of the lovely Google Truth, I was an instant convert from AssertJ. However I didn't like the boiler plate required to the get the full potential out of it. I also had just found AssertJ's code generator. So I made one for Google Truth.

You can find out everything about it on it's GitHub page.

Assertion failures for the assertions in the stated question with Truth look like:

    assertThat(car).hasName().ignoringTrailingWhiteSpace().equalTo("sally");

value of: car.getName().
expected: sally
but was : yuYU3KZU3c
car was : Car(name=yuYU3KZU3c, make=METAL, colourId=420434249)

        assertThat(car).hasMakeEqualTo(PLASTIC);

expected Make to be equal to: PLASTIC
but was                     : Car(name=7lvPSNeMJj, make=PLASTIC, colourId=1732244871)

        assertThat(car).hasColourId().isAtLeast(8);

value of               : car.getColourId()
expected to be at least: 8
but was                : 5
car was                : Car(name=oYZIeRofZn, make=PLASTIC, colourId=5)

To achieve the above, you simply add it to your build.

<plugin>
    <groupId>io.stubbs.truth</groupId>
    <artifactId>truth-generator-maven-plugin</artifactId>
    <configuration>
        <classes>
            <param>io.stubbs.truth.generator.example.Car</param>
        </classes>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
        </execution>
    </executions>
</plugin>

It is very powerful and flexible. Very keen to get feedback and implement more ideas.

Antony Stubbs
  • 13,161
  • 5
  • 35
  • 39