我正在用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
答案 0 :(得分:1)
我猜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);