Spring boot test with AttributeConverter not working with Maven test

I have Spring boot unit tests (class annotated with @SpringBootTest) that run just fine when using my IDE. When I run them with Maven (mvn test), they all fail for the same reason :

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘entityManagerFactory’ defined in class path resource [my/package/config/DatasourceConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: Unable to instantiate AttributeConverter [my.package.enum.AbstractEnumConverter] Caused by: org.hibernate.AnnotationException: Unable to instantiate AttributeConverter [my.package.enum.AbstractEnumConverter] Caused by: java.lang.NoSuchMethodException: my.package.AbstractEnumConverter.<init>()

I use a generic AbstractEnumConverter to convert my enums on my entities. It looks like this :

@Converter
public abstract class AbstractEnumConverter<T extends Enum<T> & EnumInterface<E>, E> implements AttributeConverter<T, E> {

    private static Logger logger = LoggerFactory.getLogger(AbstractEnumConverter.class);
    private final Class<T> clazz;

    public AbstractEnumConverter(Class<T> clazz) {
        this.clazz = clazz;
    }

    @Override
    public E convertToDatabaseColumn(T attribute) {
        return attribute != null ? attribute.getKey() : null;
    }

    @Override
    public T convertToEntityAttribute(E dbData) {
        if (dbData == null)
            return null;

        T[] enums = clazz.getEnumConstants();

        for (T e : enums) {
            if (e.getKey().equals(dbData))
                return e;
        }

        logger.info("Could not find value {} for class {}",dbData,clazz.getName());
        return null;
    }
}

Here is an example of an enum I use :

public enum ExampleEnum implements EnumInterface<Integer> {

    KEY(1);

    private final Integer key;

    ExampleEnum(Integer key) {
        this.key = key;
    }

    @Override
    public Integer getKey() {
        return key;
    }
    
    public static class Converter extends AbstractEnumConverter<ExampleEnum, Integer> {
        public Converter() {
            super(ExampleEnum.class);
        }
    }
}

It seems like maven tries to instantiate my AbstractEnumConverter class for some reasons unknown to me. Everything works fine with IDE unit testing and when I run my application.

Answer

Hibernate interprets the @Converter annotation as saying that it should instantiate your class and register it, but it’s an abstract class with no default constructor (your missing <init>()). Perhaps Hibernate ought to realize and exclude annotated abstract classes, but it doesn’t, so you should remove the annotation from the abstract class and add it to the base classes.