相关文章推荐
追风的毛衣  ·  constraintlayout.layou ...·  1 年前    · 
儒雅的企鹅  ·  git ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm writing some JUnit tests with Mockito and Podam. Lots of stuff working fine, but I ran into an issue in one test case while generating a recursive object.

Code like the following throws PodamMockeryException with podam 7.0.0.RELEASE on the manufacturePojo() line:

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

More stack trace and the entity are below.

Entity is a JPA entity and it looks a bit like the following:

public class Entity extends AllTheEntities {
    @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();

A bit more stack trace:

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

I guess FieldData object contains java.sql.Timestamp or extends it. The latter requires very specific values for its setters. So there is a need for customized random values tailored for this class. Here is an example, how this can be achieved:

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

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.