Home > Software design >  How to create object for Polymorphic Java Object with String JsonSubTypes
How to create object for Polymorphic Java Object with String JsonSubTypes

Time:11-25

This class is used to create a Java Object from a JSON Input. I want to create a static object for my unit test case. Here is my class :

@Data
@NoArgsConstructor
@JsonTypeInfo(
    use = JsonTypeInfo.Id.NAME,
    include = JsonTypeInfo.As.PROPERTY,
    property = "type")
@JsonSubTypes({
    @JsonSubTypes.Type(value = FullScan.class, name = "FullScan"),
    @JsonSubTypes.Type(value = PartitionBased.class, name = "PartitionBased")
})
public class BatchSpec {
    @NonNull private String type;
}

I want to create an object in my Test class with type="FullScan"

Here is how i try to do :

private static final BatchSpec batchSpec = new BatchSpec();

But it does not accept any parameters.

What i want to do is, create an object for the below json :

"batch": {"type": "FullScan"}

CodePudding user response:

Try this

BatchSpec batchSpec = new BatchSpec();
batchSpec.setType("FullScan");
JobConfig jobConfig = new JobConfig("Something",batchSpec);
  • Related