我有这样的事情:
@XmlRootElement(name = "attribute", namespace = "ns.attrib")
public class Attribute implements AuthEntity {
private String name;
private T value;
@XmlAttribute
@Override
public String getName() {
return name;
}
@Override
public Optional> getAuthEntities() {
return Optional.empty();
}
@XmlAttribute
public T getValue() {
return value;
}
public void setValue(T value) {
this.value = value;
}
public void setName(String name) {
this.name = name;
}
@Override
public int hashCode() {
return Objects.hash(getName(), getValue());
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
@SuppressWarnings("rawtypes")
GSAttribute other = (GSAttribute) obj;
if (!name.equals(other.name))
return false;
if (value == null) {
if (other.value != null)
return false;
} else if (!value.equals(other.value))
return false;
return true;
}
}
我必须使用一些自定义接口在基于xml的持久层上进行一些CRUD,所以我必须使用JAXB(版本2.2.11)来编组/解编上述类型。 好的,让我们开始进行一些测试来对此充满信心。 我写了几篇:
@Test
public void deserializeEmptyAttribute() throws JAXBException {
String xml = "";
Attribute expected = new Attribute<>();
Attribute actual = JAXB.unmarshal(new StringReader(xml), Attribute.class);
Assert.assertEquals(expected, actual);
}
@Test
public void deserializeIntegerAttribute() throws Exception {
String xml = "";
Attribute expected = new Attribute<>();
expected.setName("somename");
expected.setValue(105);
Attribute actual = JAXB.unmarshal(new StringReader(xml), Attribute.class);
Assert.assertEquals(expected, actual);
}
@Test
public void deserializeStringAttribute() {
String xml = "";
Attribute expected = new Attribute<>();
expected.setName("somename");
expected.setValue("somevalue");
Attribute actual = JAXB.unmarshal(new StringReader(xml), Attribute.class);
Assert.assertEquals(expected, actual);
}
每个测试在此堆栈上均失败:
java.lang.NullPointerException
at com.sun.xml.bind.v2.runtime.reflect.TransducedAccessor.get(TransducedAccessor.java:167)
at com.sun.xml.bind.v2.runtime.property.AttributeProperty.(AttributeProperty.java:91)
at com.sun.xml.bind.v2.runtime.property.PropertyFactory.create(PropertyFactory.java:108)
at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.(ClassBeanInfoImpl.java:181)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:503)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.(JAXBContextImpl.java:320)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.(JAXBContextImpl.java:139)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1138)
at com.sun.xml.bind.v2.ContextFactory.createContext(ContextFactory.java:162)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:247)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:234)
at javax.xml.bind.ContextFinder.find(ContextFinder.java:441)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:641)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:584)
at javax.xml.bind.JAXB$Cache.(JAXB.java:112)
at javax.xml.bind.JAXB.getContext(JAXB.java:139)
at javax.xml.bind.JAXB.unmarshal(JAXB.java:242)
...
我对JAXB还是很陌生(大多数年前都被遗忘了),但在运行正常的测试时遇到了一些麻烦。 我想念什么? 我看到有很多类似的帖子/答案,但是它们没有帮助(或者我没有得到)。