mtedone / podam

PODAM - POjo DAta Mocker
https://mtedone.github.io/podam
MIT License
326 stars 749 forks source link

Cannot switch off initialization for java.lang.Object type fields #214

Closed Amberize closed 8 years ago

Amberize commented 8 years ago

I'm using PodamFactory with my DataProvider which extending AsbractRandomDataProviderStrategy for filling my POJO. But after I need my POJO in JAXB serialization, which cannot handle java.langObject as org.w3c.dom.Element and gives me this error:

java.lang.Object incompatible with org.w3c.dom.Element

I tried to search for solution in code, but as I can understand this part of code ruins my intensions:

private <T> T instantiatePojo(Class<T> pojoClass,
            ManufacturingContext manufacturingCtx, Map<String, Type> typeArgsMap,
            Type... genericTypeArgs)
            throws SecurityException {
...
retValue = (T) constructor.newInstance(parameterValues);

Is there a way to switch off java.lang.Object field initialization and let it stay null?

daivanov commented 8 years ago

Hi,

Could you please give more context on the problem. Such as sample POJO or something, which helps to reproduce the problem.

For example, search in Google for a phrase java.lang.Object incompatible with org.w3c.dom.Element returns no results. So probably what you are getting is java.lang.ClassCastException: java.lang.Object cannot be cast to org.w3c.dom.Element. Then could you please paste a stack trace?

Thanks, Daniil

Amberize commented 8 years ago

My POJOs:

class PersonJob {

private PersonAddress address;

//a buch of fields (non Object type)
}

class PersonAddress extends AbstractContact {
//empty fields class
}

class AbstractContact {
private Object contactId;
}

So contactid field of AbstractContact is initialized with new Object() instance.

When JAXB tries to marshall PersonJob I got:

java.lang.ClassCastException: java.lang.Object incompatible with org.w3c.dom.Element
                at com.sun.xml.bind.v2.runtime.AnyTypeBeanInfo.serializeURIs(AnyTypeBeanInfo.java:153)
                at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:677)
                at com.sun.xml.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody(SingleElementNodeProperty.java:158)
                at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:360)
                at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:696)
                at com.sun.xml.bind.v2.runtime.property.ArrayElementNodeProperty.serializeItem(ArrayElementNodeProperty.java:69)
                at com.sun.xml.bind.v2.runtime.property.ArrayElementProperty.serializeListBody(ArrayElementProperty.java:172)
                at com.sun.xml.bind.v2.runtime.property.ArrayERProperty.serializeBody(ArrayERProperty.java:159)
                at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:360)
                at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsSoleContent(XMLSerializer.java:593)
                at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeRoot(ClassBeanInfoImpl.java:341)
                at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:494)
                at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:323)
                at com.sun.xml.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:251)
                at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:128)
                at ru.sbrf.bh.banking.product.pcard.branchpcard.orchestration.ValidatePersonProcess.marshallObject(ValidatePersonProcess.java:76)
                at ru.sbrf.bh.banking.product.pcard.branchpcard.orchestration.ValidatePersonProcess.validatePerson(ValidatePersonProcess.java:60)
                at ru.sbrf.bh.banking.product.pcard.branchpcard.service.flow.card.PersonValidationTest.testFilledValidation(PersonValidationTest.java:74)
                at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:95)
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:56)
                at java.lang.reflect.Method.invoke(Method.java:620)
                at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
                at org.testng.internal.Invoker.invokeMethod(Invoker.java:643)
                at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:820)
                at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1128)
                at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
                at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
                at org.testng.TestRunner.privateRun(TestRunner.java:782)
                at org.testng.TestRunner.run(TestRunner.java:632)
                at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
                at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
                at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
                at org.testng.SuiteRunner.run(SuiteRunner.java:268)
                at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
                at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
                at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
                at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
                at org.testng.TestNG.run(TestNG.java:1064)
                at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
                at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:122)
                at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:95)
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:56)
                at java.lang.reflect.Method.invoke(Method.java:620)
                at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
daivanov commented 8 years ago

Podam operates on setters and getters.

This class won't get any values to contactId injected.

class AbstractContact {
    private Object contactId;
}

I guess you also have some setters there as well.

daivanov commented 8 years ago

I tried reproduced a problem in commit d47528fbc6c86a66569e9e0bb8c61a870df3989a and it worked just as expected. Please amend my commit with missing details to reproduce the problem.

Amberize commented 8 years ago

Surely, AbstractContact POJO has getter and setter for contactId field. My fault to forget it.

class AbstractContact {
    private Object contactId;

    public Object getContactId() { return this.contactId }

    public void setContactId(Object contactId) {this.contactId = contactId}
}

So my main problem is to find a way to switch off instantiation of contactId filed via setter (as you told). I cannot use annotations as the AbstractContact class is placed in external library. Thus I'm trying to get it work with DataProvider.

daivanov commented 8 years ago

Ok, there is no really an issue here. Please, ask this question on StackOverflow under jemos-podam tag and I will gladly reply.

Amberize commented 8 years ago

Posted the question http://stackoverflow.com/questions/39034570.

@daivanov Thank you for your help and attention!

daivanov commented 8 years ago

I hope my answer helps!