Search code examples
javers

javers complains when a field is declared as Collection but initialized as concrete type


Using javers 1.4.1.

I have a field in a POJO declared like this:

private Collection<String> names;

and then initialized with an instance of a concrete Collection class like this:

names = new ArrayList<>();

When I run tests against this class, javers complains with the following error:

JaversException: MANAGED_CLASS_MAPPING_ERROR given javaClass 'class java.util.ArrayList is mapped to java.util.ArrayList, expected ManagedType

The same error if I initialize it inline, like:

private Collection<String> names = new ArrayList<>();

On the other hand, if I declare the field as a List then javers is happy:

private List<String> names;

I'm a total newb to javers, it was introduced to the project by someone else. But all I did was add this new field to an existing POJO. What do I need to look at and/or tweak to make javers happy?


Solution

  • The reason is, JaVers has to know (statically) if it is a List or Set. Without that information, JaVers doesn't know how to compare two collections. So you have to change your POJO's.

    This error message is a bit misleading, there is an issue for that here https://github.com/javers/javers/issues/353

    I would expect something like High-level Collection interface is not supported, try at least Set or List