[英]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.