Is it possible to add, or do we already have, a functionality such as isNullOr() for an intermediate checkpoint?
Note that this is not related to DB regardless the example code.
final MyEntity entity = new MyEntity();
entityManager().persist(entity);
// at this point, the createdBy attribute may be or may not be null
assertThat(entity.getCreatedBy())
.isNullOr() // stops here when the value is null.
.isEqualsTo("John");
// or
assertThat(entity.getCreatedBy())
.isNullOr(a -> { // with an assert; or with the assert
// a.isNotNull(); // obviously!
a.isEqualsTo("John");
});
Nothing specific like that but you have the more generic satisfiesAny which basically works like:
TolkienCharacter frodo = new TolkienCharacter("Frodo", HOBBIT);
Consumer<TolkienCharacter> isHobbit = tolkienCharacter -> assertThat(tolkienCharacter.getRace()).isEqualTo(HOBBIT);
Consumer<TolkienCharacter> isElf = tolkienCharacter -> assertThat(tolkienCharacter.getRace()).isEqualTo(ELF);
// assertion succeeds:
assertThat(frodo).satisfiesAnyOf(isElf, isHobbit);
In your case it would look like:
assertThat(entity.getCreatedBy()).satisfiesAnyOf(
createdBy -> assertThat(createdBy).isNull(),
createdBy -> assertThat(createdBy).isNotNull().isEqualsTo(John)
);
Not as elegant as isNullOr but does the job. (isNullOr could be renamed as isNullOrSatisfies btw)
@joel-costigliola Thank you. This can be closed and I'm doing it.
Most helpful comment
@joel-costigliola Thank you. This can be closed and I'm doing it.