Avoid setting field with reflection on a JDK class - fails on newer JDK versions

This commit is contained in:
ljacqu 2023-06-20 17:03:06 +02:00
parent 2e3448ab58
commit db04fd2a5b

View File

@ -40,10 +40,10 @@ public class ExceptionUtilsTest {
@Test
public void shouldHandleCircularCausesGracefully() {
// given
IllegalStateException ise = new IllegalStateException();
UnsupportedOperationException uoe = new UnsupportedOperationException(ise);
ExceptionWithSettableCause exceptionWithSettableCause = new ExceptionWithSettableCause();
UnsupportedOperationException uoe = new UnsupportedOperationException(exceptionWithSettableCause);
ReflectiveOperationException roe = new ReflectiveOperationException(uoe);
ReflectionTestUtils.setField(Throwable.class, ise, "cause", roe);
exceptionWithSettableCause.cause = roe;
// when
NullPointerException resultNpe = ExceptionUtils.findThrowableInCause(NullPointerException.class, uoe);
@ -65,4 +65,14 @@ public class ExceptionUtilsTest {
// then
assertThat(result, equalTo("[MalformedURLException]: Unrecognized URL format"));
}
private static final class ExceptionWithSettableCause extends Exception {
Exception cause;
@Override
public synchronized Throwable getCause() {
return cause;
}
}
}