So this piece of code
public boolean getBoolean(Integer Id) {
return Optional.of(Store.getMagazin(Id))
.map(MagazinDO::getBoolean)
.orElse(Boolean.FALSE);}
returns a null while this one doesn't
public boolean getBoolean(Integer Id) {
var magazin = store.getMagazin(Id);
if (magazin != null) {
boolean = magazin.getBoolean();
return boolean != null ? oppositeConversion : false;
}
return false;
}
Why is this happening? I thought optional chaining can handle nulls.
As @Chaosfire said, there is no way that your
getOppositePriceConversion(Integer catalogId)returns null, so if you meant thatcatalogStore.getCatalogthrows aNullPointerException, you can handle it viaOptional.ofNullable. I'm pretty sure about that, I enjoy to create some test case about it, it was a nice excuse to play with Mokito !Given that structure:
Then the tests:
fyi: java 17, junit 5.10.1, assterj 3.25.1, mokito 5.2.0