gpt4 book ai didi

java - 关闭 podam 中的 java.langObject 初始化

转载 作者:行者123 更新时间:2023-11-30 10:40:10 28 4
gpt4 key购买 nike

我有 POJO:

class PersonJob {

private PersonAddress address;

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

class PersonAddress extends AbstractContact {
//empty fields class
}

class AbstractContact {
private Object contactId;

public Object getContactId() { return this.contactId }

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

AbstractContact 类放在外部库中。

我将 PodamFactory 与我的 DataProvider 一起使用,它扩展了 AsbractRandomDataProviderStrategy 以填充 PersonJob。但是当 JAXB 序列化它时,我会遇到这样的错误:

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)

有没有办法通过扩展 AsbractRandomDataProviderStrategy 的 DataProvider 关闭 AbstractContact 类的 contactId 字段的实例化?

最佳答案

首先,您能否将contactId 更改为实际类型?我敢打赌在运行时它不是一个 Object,但正如它所声明的那样,它会让 Podam 感到困惑。

如果前者不是一个选项,那么您可以做三件事:

  1. 使用 @PodamExclude 注解排除 contactId 字段
  2. 使用类信息策略排除 contactId 字段

    DefaultClassInfoStrategy classInfoStrategy = DefaultClassInfoStrategy.getInstance();classInfoStrategy.addExcludedField(PersonAddress.class, "contactId");podamFactory.setClassStrategy(classInfoStrategy);

  3. 只需在制造后调用 pojo.setContactId(null); 或者更好 pojo.setContactId(yourActualAddressObject);

关于java - 关闭 podam 中的 java.langObject 初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39034570/

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