Kenny Weeler
Kenny Weeler

Reputation: 163

Json Deserialization Error Spring Boot Testing - Cannot construct the instance

I have a problem and below is code:-

MainService.java

public void someMethod(Message message){

A aObj = new A (someEnumClass.enumValue, message)

someService.saveData(aObj)

}

public Class A extends B {

int x,y,z;

A(SomeEnumClass enumValue,int c){

super(enumValue);

}

Public abstract class B<EnumClass extends Enum<EnumClass>>{

private EnumClass enumValue

}

Now the above code works:

But when I test using my below class

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class MyDemoTests {
  @Autowired
    private SomeService someService;

    @Autowired
    private someRepo repository;


    @Test
        public void testMyservice() throws Exception {
            List<A> results = repository.findAll();
            assertThat(results).containsAll(all);

        }

I get the below error on "results":-

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `java.lang.Enum` (no Creators, like default construct, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information
 at [Source: (InputStreamReader); line: 1, column: 123] (through reference chain: com.example.A["enumValue"])

Much Appreciated. Thanks

Upvotes: 0

Views: 552

Answers (1)

jokster
jokster

Reputation: 577

AFAIK, you must not extend Enum like you do! The only allowed way is public enum .... Therefore it is aly not possible to create an abstract enum base class!

Depending on what you really want to do, you might be able to achieve the same with an interface containing default methods. An enum can implements interfaces.

Upvotes: 1

Related Questions