0

我正在使用 PODAM 库将虚拟数据生成到我的 POJO 类中。当该类具有通用类类型变量时,我在这样做时遇到了一个问题。例如:

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

MyGenericClass如下所示,

public class MyGenericClass<T>
{
    protected int status;

    // standard getters and setters
}

使用 PODAM填充MyClassToBeFilledWithPodam类时,会引发以下异常。

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

到目前为止,我所做的是在填充然后分别填充通用对象时忽略此字段,如下所示。

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 ) );
    }
}

但是对项目中所有可用的类都这样做是不可能的。有没有办法配置 PODAM 来说明对于 Generic classes MyGenericClass,使用默认值或指示它正确填充。

4

1 回答 1

1

我不得不用 arrayList 做类似的事情。一探究竟

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

我想你应该试试

podamFactory.manufacturePojo(MyClassToBeFilledWithPodam.class, DesiredGenericClassType.class);
于 2021-06-02T03:28:22.623 回答