在测试中执行assertThat isEqualTo 时出现InaccessibleObjectException

问题描述 投票:0回答:1

运行测试来比较 2 个 XML 对象时出现此错误 -

org.assertj.core.util.introspection.IntrospectionError: 
Can't find any field or property with name 'orig_second'.
Error when introspecting properties was :
- No getter for property 'orig_second' in com.sun.org.apache.xerces.internal.jaxp.datatype.XMLGregorianCalendarImpl 
Error when introspecting fields was :
- Unable to obtain the value of the field <'orig_second'> from <2023-06-06T16:58:29.053Z>

Caused by: org.assertj.core.util.introspection.IntrospectionError: Unable to obtain the value of the field <'orig_second'> from <2023-06-06T16:58:29.053Z>
    at org.assertj.core.util.introspection.FieldSupport.readSimpleField(FieldSupport.java:248)
    at org.assertj.core.util.introspection.FieldSupport.fieldValue(FieldSupport.java:202)
    at org.assertj.core.util.introspection.PropertyOrFieldSupport.getSimpleValue(PropertyOrFieldSupport.java:75)
    ... 50 more
Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make field private transient int com.sun.org.apache.xerces.internal.jaxp.datatype.XMLGregorianCalendarImpl.orig_second accessible: module java.xml does not "opens com.sun.org.apache.xerces.internal.jaxp.datatype" to unnamed module @36c88a32
    at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:354)
    at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297)
    at java.base/java.lang.reflect.Field.checkCanSetAccessible(Field.java:178)
    at java.base/java.lang.reflect.Field.setAccessible(Field.java:172)
    at org.assertj.core.util.introspection.FieldUtils.getField(FieldUtils.java:67)
    at org.assertj.core.util.introspection.FieldUtils.readField(FieldUtils.java:143)
    at org.assertj.core.util.introspection.FieldSupport.readSimpleField(FieldSupport.java:208)
    ... 52 more

使用Java 17

几天前正在工作,但突然收到此错误,尚未更改任何库版本或代码。

java-17 xerces jaxp
1个回答
0
投票

尝试使用

withEqualsForType(Objects::equals,XMLGregorianCalendar.class)

例如:

assertThat(actual).usingRecursiveComparison()
                                .withEqualsForType(Objects::equals,XMLGregorianCalendar.class)
                                .isEqualTo(expected);
© www.soinside.com 2019 - 2024. All rights reserved.