1

I am getting an exception when running an integration test with gradle.

The exception I'm getting is:

com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of AbstractCommonDetails: abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information

The Summary class has the following annotations on the details I am trying to deserialize:

@Data
public class Summary {
  @JsonTypeInfo(use = NAME, include = PROPERTY, property = "@type")
  @JsonSubTypes({
      @JsonSubTypes.Type(value = CardDetails.class, name = "CARD")
  })
  private AbstractCommonDetails details;
}

AbstractCommonDetails looks like this:

@Getter
@Setter
public abstract class AbstractCommonDetails {
  private Long id;
  public abstract String getPaymentMethod();
}

And the CardDetails has @JsonTypeName("CARD") and @Data annotations and the implementation of getPaymentMethod method.

ObjectMapper configuration and deserializing is done as follows:

new ObjectMapper().findAndRegisterModules().readValue(IOUtils.toString(inputStream, "UTF-8"), Summary.class);

And sample json:

{
  "details":{
    "@type":"CARD",
    "amount":"10.00"
}

Weird thing is - running the integration test as a Junit test succeeds but when running the same integration test with gradlew test command it fails with the exception mentioned before. What could be the cause?

Janar
  • 2,623
  • 1
  • 22
  • 32
  • Could you show us how `AbstractCommonDetails` class looks like? How do you run `Junit` test? In `IDE` or `Maven`? – Michał Ziober Feb 25 '19 at 13:07
  • Junit test is run in IDE. Posted the code of AbstractCommonDetails – Janar Feb 25 '19 at 13:54
  • if it does not work with `gradle` it means we need to configure somehow `Lambok` annotations to work. Could you check for example that [annotationProcessor gradle 4.7+ configuration doesn't run lombok](https://stackoverflow.com/questions/50519138/annotationprocessor-gradle-4-7-configuration-doesnt-run-lombok) – Michał Ziober Feb 25 '19 at 14:18
  • 1
    I'll try removing lombok annotations and running the tests to verify if that is where the problem lies. – Janar Feb 25 '19 at 14:39
  • 1
    @MichałZiober removing lombok annotations indeed fixed the problem - I'll try getting it to work using the example with lombok. thanks! – Janar Feb 25 '19 at 14:51

1 Answers1

0

If test works on IDE but does not work with gradle it means you need to take a look on gradle configuration. You use Lambook, so you need to be sure that gradle configuration is aware about Lambok annotations.

See also:

Michał Ziober
  • 37,175
  • 18
  • 99
  • 146