gpt4 book ai didi

java - 在 PODAM 7.0.0 中创建递归对象时出现 PodamMockeryException

转载 作者:行者123 更新时间:2023-12-02 03:00:01 25 4
gpt4 key购买 nike

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

类似下面的代码在 manufacturePojo() 行上使用 podam 7.0.0.RELEASE 抛出 PodamMockeryException:

@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

最佳答案

我猜 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);

关于java - 在 PODAM 7.0.0 中创建递归对象时出现 PodamMockeryException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42492135/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com