简体   繁体   中英

XStream: could not access field with implementation of abstract class

In my project I use XStream to serialize/deserialize my entities.
Everythings works fine in the application but not when I trie to create some unit tests.

I have an abstract class A with a field name and public accessors. I have a class B that extends A .
My XStream configuration is like this one:

XStream xStream = new XStream();
//
xStream.alias("classNameSerialized", AnotherClass.class);
xStream.aliasAttribute(AnotherClass.class, OBJECT_ATTRIBUT_VERSION, OBJECT_ATTRIBUT_VERSION);
...
//
xStream.alias("classNameSerialized", B.class);
xStream.aliasAttribute(B.class, "name", "name");

When I run my application, there is no problem. But when I try to create an unit test, XStream fires the exception: IllegalArgumentException: could not access org.mycompagny.B.name field: name

EDIT

I tried in my test B.class.getDeclaredField("name"); and it throws me the same exception. But when I try A.class.getDeclaredField("name"); there is no problem. I understand that it can be a problem whith the reflection, but what I cannot understand is why this problem appears just in my unit test and not in my application...

The final question is how can I test this code???

To resolve my problem I had to delete my abstract class...
In my case it was not a big problem, only 5 classes inherited from the abstract class.
If somebody has a better solution, post it here ;-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM