Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am using the PODAM library to generate dummy data into my POJO classes. I ran into an issue while doing that when the class has a Generic class type variables. For example:

public class MyClassToBeFilledWithPodam{
  List<MyGenericClass> statusList;
  // standard getters and setters

And MyGenericClass is as below,

public class MyGenericClass<T>
    protected int status;
    // standard getters and setters

When populating the MyClassToBeFilledWithPodam class with PODAM, the Following exception gets thrown.

Caused by: java.lang.IllegalArgumentException: com.example.MyGenericClass is missing generic type arguments, expected [T], provided []
        at uk.co.jemos.podam.typeManufacturers.TypeManufacturerUtil.fillTypeArgMap(TypeManufacturerUtil.java:200)
        at uk.co.jemos.podam.api.PodamFactoryImpl.manufacturePojoInternal(PodamFactoryImpl.java:491)
        at uk.co.jemos.podam.api.PodamFactoryImpl.manufactureAttributeValue(PodamFactoryImpl.java:964)
        at uk.co.jemos.podam.api.PodamFactoryImpl.fillCollection(PodamFactoryImpl.java:1230)
        at uk.co.jemos.podam.api.PodamFactoryImpl.resolveCollectionValueWhenCollectionIsPojoAttribute(PodamFactoryImpl.java:1080)
        ... 56 more

So far what I did was to ignore this field while populating and then populating the generic object separately like below.

class MyTest
    @Test
    void testMethod() throws Exception
        MyClassToBeFilledWithPodam myClassTobeFilledWithPodam = new MyClassToBeFilledWithPodam();
        PodamFactory factory = new PodamFactoryImpl();
        DefaultClassInfoStrategy classInfoStrategy = DefaultClassInfoStrategy.getInstance();
        classInfoStrategy.addExcludedField( MyClassToBeFilledWithPodam.class, "statusList" );
        factory.setClassStrategy( classInfoStrategy );
        myClassTobeFilledWithPodam = factory.populatePojo( myClassTobeFilledWithPodam ); // Generate class here
        MyGenericClass deleted = factory.manufacturePojo( MyGenericClass.class, String.class ); // T can be anything because all I want is the status value. Therefore I pass String as the type 
        myClassTobeFilledWithPodam.setStatusList( List.of( deleted ) );

But doing this to all the available classes in the project is impossible. Is there a way to configure PODAM to say that for the Generic classes MyGenericClass, use a default value or instruct it to populate correctly.

I had to do something similar to it with an arrayList. Check it out

var projectsResponse = podamFactory.manufacturePojo(ArrayList.class, ProjectResponse.class);
when(projectService.getProjectsFromAccountId()).thenReturn(projectsResponse);

I guess you should try

podamFactory.manufacturePojo(MyClassToBeFilledWithPodam.class, DesiredGenericClassType.class);
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.