0

我正在用 Mockito 和 Podam 编写一些 JUnit 测试。很多东西都可以正常工作,但是我在生成递归对象时在一个测试用例中遇到了问题。

像下面这样的代码在PodamMockeryExceptionpodam 7.0.0.RELEASE 上抛出manufacturePojo()

@Test
 public void testDoTest() {
     PodamFactory podamfactory = new PodamFactoryImpl();
     Entity eTest = podamfactory.manufacturePojo(Entity.class);
     ...
 }

更多堆栈跟踪和实体如下。

Entity 是一个 JPA 实体,它看起来有点像下面这样:

public class Entity extends AllTheEntities {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "DOCUMENT_SEQ")
    @SequenceGenerator(name = "DOCUMENT_SEQ", sequenceName = "DOCUMENT_SEQ", allocationSize = 1)
    @Column(name = "DOCUMENT_ID", precision = 19)
    private long objectId;
    @Column(name = "NAME")
    private String name;

    // bi-directional many-to-one associations
    @ManyToOne
    @JoinColumn(name = "FK_STND_MIME_TYPE_ID", nullable = false)
    private StndMimeTypeEntity stndMimeType;

    @OneToMany(mappedBy = "document", cascade = CascadeType.REMOVE)
    private List<FolderThingEntity> FolderThings;

    @OneToMany(mappedBy = "document", cascade = CascadeType.ALL)
    private List<FieldData> fieldData;

    @OneToMany(mappedBy="document")
    private List<PersonalFormContentEntity> personalFormContents;

    public Entity() {
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<FieldData> getDocumentBindFieldData() {
        return this.fieldData;
    }

    public void setDocumentBindFieldData(
            List<FieldData> fieldData) {
        this.fieldData = fieldData;
    }

    public FieldData addDocumentBindFieldData(
            FieldData fieldData) {
        getDocumentBindFieldData().add(fieldData);
        fieldData.setDocument(this);

        return fieldData;
    }

    @Override
    public String toString() {
        return "Entity [objectId=" + objectId + ", name="
    + name + ", stndMimeType=" + stndMimeType.getStndMimeTypeId() + "]";
    }

    public OfficeActionEntity getForm() {

        FolderThingEntity fd = getFolderThings().get(0);

        FolderEntity folderEntity = fd.getFolder();

        return folderEntity.getForm();

    }

}

更多堆栈跟踪:

uk.co.jemos.podam.exceptions.PodamMockeryException
    at uk.co.jemos.podam.api.PodamFactoryImpl.doManufacturePojo(PodamFactoryImpl.java:443)
    at uk.co.jemos.podam.api.PodamFactoryImpl.manufacturePojo(PodamFactoryImpl.java:148)
    at CENSORED(CENSOREDTest.java:261)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
  ... 20 more
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at uk.co.jemos.podam.api.PodamFactoryImpl.populatePojoInternal(PodamFactoryImpl.java:694)
    at uk.co.jemos.podam.api.PodamFactoryImpl.manufacturePojoInternal(PodamFactoryImpl.java:532)
    at uk.co.jemos.podam.api.PodamFactoryImpl.manufactureAttributeValue(PodamFactoryImpl.java:885)
    at uk.co.jemos.podam.api.PodamFactoryImpl.populatePojoInternal(PodamFactoryImpl.java:687)
    at uk.co.jemos.podam.api.PodamFactoryImpl.manufacturePojoInternal(PodamFactoryImpl.java:532)
    at uk.co.jemos.podam.api.PodamFactoryImpl.doManufacturePojo(PodamFactoryImpl.java:436)
    ... 30 more
Caused by: java.lang.IllegalArgumentException: nanos > 999999999 or < 0
    at java.sql.Timestamp.setNanos(Timestamp.java:386)
    ... 40 more
4

1 回答 1

2

我猜FieldData对象包含java.sql.Timestamp或扩展它。后者需要非常具体的设置器值。所以需要为这个类量身定制的随机值。这是一个示例,如何实现这一点:

PodamFactory podamFactory = new PodamFactoryImpl();
TypeManufacturer<Integer> manufacturer = new IntTypeManufacturerImpl() {

    @Override
    public Integer getInteger(AttributeMetadata attributeMetadata) {

        if (attributeMetadata.getPojoClass() instanceof Timestamp) {
            return PodamUtils.getIntegerInRange(0, 999999999);
        } else {
            return super.getInteger(attributeMetadata);
        }
    }
};
podamFactory.getStrategy().addOrReplaceTypeManufacturer(int.class, manufacturer);

FieldData pojo = podamFactory.manufacturePojo(FieldData.class);
于 2017-04-27T10:27:18.647 回答