簡體   English   中英

PodamFactory 沒有設置帶注釋的字段

[英]PodamFactory is not setting fields with annotations

我正在為我的類做單元測試,我使用 PodamFactoryImpl 來用虛擬數據填充對象。

問題:
似乎帶有@NotEmpty(來自javax.validation.constraints)注釋的字段是NULL。

有什么方法可以配置 PodamFactoryImpl 實例來填充所有字段嗎? (比如將其配置為忽略注釋)

我的填充方法:

public static <T> T fillObject(Class<T> clazz) {
    PodamFactory factory = new PodamFactoryImpl();
    return factory.manufacturePojoWithFullData(clazz);
}

我悲傷的例子 POJO:

public class Location {
    @NotEmpty
    private String tenant; //will be null
    @NotEmpty
    private String serviceName; //will be null
    @NotNull
    private List<LocationData> locations; //will be with value
    @NotNull
    private Boolean async; //will be with value
    @NotNull
    private Long companyIndex; //will be with value
    @NotEmpty
    private String topicId; //will also be null
}

您將升級神器版本

 <dependency>
    <groupId>uk.co.jemos.podam</groupId>
    <artifactId>podam</artifactId>
    <version>7.2.6.RELEASE</version>
    <scope>test</scope>
</dependency>

這是因為 BeanValidationStratagy。 這個 class 在注釋的情況下使用並生成不正確的數據。 您可以根據注釋 class 添加屬性策略,以您需要的方式自定義生成。

PodamFactoryImpl podamFactory = new PodamFactoryImpl(); 
((RandomDataProviderStrategy) podamFactory.getStrategy()).addOrReplaceAttributeStrategy(MyUrlConstraintAnnotation.class, (attrType, attrAnnotations) -> "https://www.company.com/");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM