简体   繁体   English

java.lang.ClassCastException: class com.sun.org.apache.xerces.internal.dom.ElementNSImpl cannot be cast to class

[英]java.lang.ClassCastException: class com.sun.org.apache.xerces.internal.dom.ElementNSImpl cannot be cast to class

I'm trying to write a Java unit test for the below code我正在尝试为以下代码编写 Java 单元测试

public class PrintnetRequestTranslator extends RequestTranslator<MutationOverviewDocRequest, RequestContent> {公共 class PrintnetRequestTranslator 扩展 RequestTranslator<MutationOverviewDocRequest, RequestContent> {

public PrintnetRequestTranslator(final MutationOverviewDocRequest mutationOverviewDocRequest) {
    super(mutationOverviewDocRequest);
}

private static final JAXBContext JAXB_CONTEXT;

static {
    try {
        JAXB_CONTEXT =
                JAXBContext.newInstance("printnet.generated");
    } catch (final JAXBException ex) {
        throw new CardLimitsServiceException(ex);
    }
}



private static final String KOP_FALSE = "false";

@Override
public RequestContent translateRequest() {

    final RequestContent requestContent = new RequestContent();
    requestContent.setOutputFormat(GjOutputFormatType.PDF);

    final Delivery delivery = new Delivery();

    requestContent.setDelivery(delivery);

    delivery.setCreationDateTime(LocalDateTime.now().toString());
    final Delivery.Document document = new Delivery.Document();
    document.setName("Mutatieoverzicht");
    document.setNumberOfCopies(1);
    final Delivery.Document.GenericData genericData = new Delivery.Document.GenericData();
    final Delivery.Document.GenericData.Bank bank = new Delivery.Document.GenericData.Bank();
    bank.setBankCode(String.valueOf(getRequest().getRelationDetails().getLocalBank().getBankCode()));
    final Delivery.Document.GenericData.Clients clients = new Delivery.Document.GenericData.Clients();
    final Delivery.Document.GenericData.Clients.Client client = new Delivery.Document.GenericData.Clients.Client();

    client.setId(getRequest().getCustomerRelationId());
    client.setInitials(getRequest().getRelationDetails().getNaturalPersonDetails().getNameInitial());
    client.setSurname(getRequest().getRelationDetails().getNaturalPersonDetails().getSurname());
    client.setDateOfBirth((getRequest().getRelationDetails().getBirthDate()).toString());
    client.setGender((getRequest().getRelationDetails().getGenderCode()));

    final Delivery.Document.GenericData.Clients.Client.Address address =
            new Delivery.Document.GenericData.Clients.Client.Address();
    address.setStreet(getRequest().getRelationDetails().getAddress().getStreetName());
    address.setCity(getRequest().getRelationDetails().getAddress().getCityName());
    address.setPostCode(getRequest().getRelationDetails().getAddress().getPostalCode());
    address.setCountry(getRequest().getRelationDetails().getAddress().getCountryCode());

    client.setAddress(address);
    clients.withClients(client);
    genericData.setBank(bank);
    genericData.withClients(clients);
    document.setGenericData(genericData);

    final Delivery.Document.MutatieOverzicht mutatieOverzicht = new Delivery.Document.MutatieOverzicht();
    mutatieOverzicht
        .setTitle(ACCOUNT_TYPE_MAP.getOrDefault(getRequest().getPackageProductTypeCode(), "Rabo Rekening"));
    mutatieOverzicht.setOndertekenblokIndicator(KOP_FALSE);
    mutatieOverzicht.setVerkoopOnderVoorbehoudIndicator(KOP_FALSE);
    final Delivery.Document.MutatieOverzicht.Product product = new Delivery.Document.MutatieOverzicht.Product();

    product.getDetailRegels()
        .add(getDetails(DOCUMENT_NAME_MAP.getOrDefault(getRequest().getCardProductTypeCode(), "Rabopas"),
            JOINER_SPACE_DELIMITER, DOCUMENT_PRODUCT_NEW_VALUE, "true"));
    product.getDetailRegels()
        .add(getDetails(
            new StringJoiner(JOINER_DELIMITER).add(DOCUMENT_PRODUCT_IBAN).add(getRequest().getIban()).toString(),
            JOINER_SPACE_DELIMITER, JOINER_SPACE_DELIMITER, KOP_FALSE));
    product.getDetailRegels().add(
        getDetails(DOCUMENT_PRODUCT_PASSNUMBER, getRequest().getCardNumber(), JOINER_SPACE_DELIMITER, KOP_FALSE));
    product.getDetailRegels()
        .add(getDetails(DOCUMENT_PRODUCT_GIVENNAME, JOINER_SPACE_DELIMITER,
            getProductGivenName(getRequest().getRelationDetails(), getRequest().getRetrieveDebitCardToken()),
            KOP_FALSE));
    product.getDetailRegels()
        .add(getDetails(DOCUMENT_PRODUCT_GEALIMIT,
            new StringJoiner(JOINER_SPACE_DELIMITER).add(getFormattedAmount(getRequest().getOldAtmLimit()))
                .add(DOCUMENT_PRODUCT_EURO).toString(),
            new StringJoiner(JOINER_SPACE_DELIMITER).add(getFormattedAmount(getRequest().getNewAtmLimit()))
                .add(DOCUMENT_PRODUCT_EURO).toString(),
            KOP_FALSE));
    product.getDetailRegels().add(getDetails(DOCUMENT_PRODUCT_PERIOD, getRequest().getAtmLimitPeriod(),
        getRequest().getAtmLimitPeriod(), KOP_FALSE));
    product.getDetailRegels()
        .add(getDetails(DOCUMENT_PRODUCT_BEALIMIT,
            new StringJoiner(JOINER_SPACE_DELIMITER).add(getFormattedAmount(getRequest().getOldPosLimit()))
                .add(DOCUMENT_PRODUCT_EURO).toString(),
            new StringJoiner(JOINER_SPACE_DELIMITER).add(getFormattedAmount(getRequest().getNewPosLimit()))
                .add(DOCUMENT_PRODUCT_EURO).toString(),
            KOP_FALSE));
    product.getDetailRegels().add(getDetails(DOCUMENT_PRODUCT_PERIOD, getRequest().getPosLimitPeriod(),
        getRequest().getPosLimitPeriod(), KOP_FALSE));
    mutatieOverzicht.setProduct(product);
    document.setMutatieOverzicht(mutatieOverzicht);
    delivery.withDocuments(document);
    requestContent.setDelivery(getDocument(delivery));
    return requestContent;

}



@Override
public ServiceInfo getServiceInfo() {
    return ServiceInfo.PRINTNET_GENERATE_DOCUMENT;
}

private Delivery.Document.MutatieOverzicht.Product.DetailRegel getDetails(final String productName,
                                                                          final String oldValue,
                                                                          final String newValue, final String kop)
{
    final Delivery.Document.MutatieOverzicht.Product.DetailRegel detailRegel =
            new Delivery.Document.MutatieOverzicht.Product.DetailRegel();

    detailRegel.setOmschrijving(productName);
    detailRegel.setOudeWaarde(oldValue);
    detailRegel.setNieuweWaarde(newValue);
    detailRegel.setKop(kop);
    return detailRegel;

}

private String getProductGivenName(final RelationResponse relationDetails,
                                   final RetrieveDebitCardToken retrieveDebitCardToken)
{
    final StringJoiner productGivenName = new StringJoiner(JOINER_SPACE_DELIMITER);
    if (relationDetails.getOrganisationLegalName() == null) {
        addIfNotEmpty(productGivenName, relationDetails.getNaturalPersonDetails().getNameInitial());
        addIfNotEmpty(productGivenName, relationDetails.getNaturalPersonDetails().getSurnamePrefix());
        addIfNotEmpty(productGivenName, relationDetails.getNaturalPersonDetails().getSurname());
    } else {
        addIfNotEmpty(productGivenName, retrieveDebitCardToken.getTokenText1());
    }
    return productGivenName.toString();
}

private void addIfNotEmpty(final StringJoiner stringJoiner, final String join) {
    if (StringUtils.isNotEmpty(join)) {
        stringJoiner.add(join);
    }
}

private Element getDocument(final Object any) {
    try {
        final DocumentBuilderFactory df = DocumentBuilderFactory.newInstance();
        df.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
        df.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
        final DocumentBuilder db = df.newDocumentBuilder();

        final org.w3c.dom.Document document = db.newDocument();
        final Marshaller marshaller = JAXB_CONTEXT.createMarshaller();

        marshaller.marshal(any, document);
        return document.getDocumentElement();
    } catch (final ParserConfigurationException | JAXBException ex) {
        throw new CardLimitsServiceException(ex);
    }
}

} }

Here is the RequestContent Class这是 RequestContent Class

 @XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "outputFormat",
    "delivery"
})
@XmlRootElement(name = "RequestContent")
public class RequestContent {

    @XmlElement(name = "OutputFormat", required = true)
    @XmlSchemaType(name = "string")
    protected GjOutputFormatType outputFormat;
    @XmlElement(name = "Delivery", required = true)
    protected Object delivery;

    /**
     * Gets the value of the outputFormat property.
     * 
     * @return
     *     possible object is
     *     {@link GjOutputFormatType }
     *     
     */
    public GjOutputFormatType getOutputFormat() {
        return outputFormat;
    }

    /**
     * Sets the value of the outputFormat property.
     * 
     * @param value
     *     allowed object is
     *     {@link GjOutputFormatType }
     *     
     */
    public void setOutputFormat(GjOutputFormatType value) {
        this.outputFormat = value;
    }

    /**
     * Gets the value of the delivery property.
     * 
     * @return
     *     possible object is
     *     {@link Object }
     *     
     */
    public Object getDelivery() {
        return delivery;
    }

    /**
     * Sets the value of the delivery property.
     * 
     * @param value
     *     allowed object is
     *     {@link Object }
     *     
     */
    public void setDelivery(Object value) {
        this.delivery = value;
    }

    public RequestContent withOutputFormat(GjOutputFormatType value) {
        setOutputFormat(value);
        return this;
    }

    public RequestContent withDelivery(Object value) {
        setDelivery(value);
        return this;
    }

}

Here is my unit test code这是我的单元测试代码

class PrintnetRequestTranslatorTest { class PrintnetRequestTranslatorTest {

private PrintnetRequestTranslator classUnderTest;

@BeforeEach
void setup() {
    classUnderTest = new PrintnetRequestTranslator(TestData.mockMutationDocumentRequest());
}

@Test
@DisplayName("Printnet Translator Success Scenario")
void testPrintnetRequestSuccess() {
    final var result = classUnderTest.translateRequest();
    final Delivery delivery = (Delivery) result.getDelivery();
    assertAll("Printnet Request Translator Test : ",
        () -> assertEquals(TestUtils.PRINTNET_OUTPUT_FORMAT, result.getOutputFormat().toString()));

}

} }

I'm getting the below error when I' trying to cast Delivery class.当我尝试投射 Delivery class 时出现以下错误。 java.lang.ClassCastException: class com.sun.org.apache.xerces.internal.dom.ElementNSImpl cannot be cast to class printnet.generated.Delivery (com.sun.org.apache.xerces.internal.dom.ElementNSImpl is in module java.xml of loader 'bootstrap'; ......printnet.generated.Delivery is in unnamed module of loader 'app')

How can I solve this error/write a unit test for the above class?如何解决此错误/为上述 class 编写单元测试?

I had the same exception, and noticed, that my unmarshaller left the ElementNSImpl instead of my own class in the list.我有同样的例外,并注意到,我的解组器在列表中留下了 ElementNSImpl 而不是我自己的 class。

The second answer in this post Intermittent ClassCastException from ElementNSImpl to own type during unmarshalling solved it for me.这篇文章中的第二个答案从 ElementNSImpl 到在解组期间拥有类型的间歇性 ClassCastException为我解决了这个问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型为ElementNSImpl的对象作为com.sun.org.apache.xerces.internal.dom.ElementNSImpl - Object of type ElementNSImpl coming as com.sun.org.apache.xerces.internal.dom.ElementNSImpl Java XML: ClassCastException class com.sun.org.apache.xerces.internal.dom.DeferredTextImpl cannot be cast to class javax.swing.text.Element - Java XML: ClassCastException class com.sun.org.apache.xerces.internal.dom.DeferredTextImpl cannot be cast to class javax.swing.text.Element java.lang.ClassCastException: com.sun.proxy.$Proxy8 不能转换为 org.openqa.selenium.internal.WrapsDriver - java.lang.ClassCastException: com.sun.proxy.$Proxy8 cannot be cast to org.openqa.selenium.internal.WrapsDriver java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap 无法转换为模型类 android - java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to model class android java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to pojo.class - java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to pojo.class 线程“main”中的异常 java.lang.ClassCastException:类 com.sun.proxy.$Proxy0 不能转换为类船员 - Exception in thread “main” java.lang.ClassCastException: class com.sun.proxy.$Proxy0 cannot be cast to class crewcut com.sun.org.apache.xerces.internal.dom.DocumentImpl无法转换为org.codehaus.plexus.util.xml.Xpp3Dom - com.sun.org.apache.xerces.internal.dom.DocumentImpl cannot be cast to org.codehaus.plexus.util.xml.Xpp3Dom 类Cast异常:com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl - Class Cast exception: com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl java:java.lang.ClassCastException:com.sun.proxy.$Proxy0 类无法转换为 com.sun.tools.javac.processing.JavacProcessingEnvironment 类 - java: java.lang.ClassCastException: class com.sun.proxy.$Proxy0 cannot be cast to class com.sun.tools.javac.processing.JavacProcessingEnvironment java.lang.ClassCastException:org.hibernate.internal.QueryImpl无法转换为com.models.User - java.lang.ClassCastException: org.hibernate.internal.QueryImpl cannot be cast to com.models.User
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM