How can I check particular field value in my custom excepiton using assertJ?
Here is exception class:
public class SomeException extends RuntimeException {
private final Set<Integer> something;
public SomeException (String message, Set<Integer> something) {
super(message);
this.something = something;
}
public Set<Integer> getSomething() {
return something;
}
}
Here is my test:
assertThatThrownBy(() -> service.doSomething())
.isInstanceOf(SomeException.class)
.hasMessageStartingWith("SomeException has 1,2,3,4 in something field. I want assert that")
. ??? check that SomeException.getSomething() has 1,2,3,4 ???
The problem is that if I chain extracting() it will think I'm using Throwable. So I can't extract field something
Update:
SomeException throwable = (SomeException) catchThrowable(() -> service.doSomething(
assertThat(throwable)
.hasMessageStartingWith("extracting() bellow still think we're working with Throwable")
.extracting(SomeException::getSomething <<<--- doesn't work here)
I have tried following, as suggested bellow:
assertThat(throwable)
.hasMessageStartingWith("Works except containsExactlyInAnyOrder()")
.asInstanceOf(InstanceOfAssertFactories.type(SomeException.class))
.extracting(SomeException::getSomething)
.->>>containsExactlyInAnyOrder<<<--- Not working!!!
But I can't use containsExactlyInAnyOrder() anymore :(
Please advise
Throwable
usingcatchThrowable
and then make whatever assertions you want to on that (after casting to your type if necessary). – Laurenelaurens