我正在用Mockito和Podam编写一些Junit测试。很多东西正常工作,但是我在一个测试案例中遇到了一个问题,同时生成递归对象。
类似以下代码将PodamMockeryException
与podam 7.0.0.Release在manufacturePojo()
行上:
@Test
public void testDoTest() {
PodamFactory podamfactory = new PodamFactoryImpl();
Entity eTest = podamfactory.manufacturePojo(Entity.class);
...
}
更多的堆栈跟踪和实体在下面。
实体是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
我猜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);