I have two classes:
class Outer {
Inner inner = new Inner("value");
}
class Inner {
private final String value;
Inner(String value) {
this.value = value;
}
}
public Optional<Outer> getOptionalValue() {
return Optional.of(new Outer());
}
And my test:
public void testCLass() {
Assertions.assertThat(getOptionalValue())
.isPresent()
.map(v -> v.inner.value)
.isEqualTo("value");
}
I expect it to pass, because isPresent
unwraps optional, map
converts Outer
into value
and in the last method I just compare strings.
But it fails on the last line with the message:
Expecting:
<Optional[value]>
to be equal to:
<"value">
but was not.
I have no idea why the optional is not unwrapped
.map(v -> v.inner.value)
I think you are using getter to access to the value – Irradiate