我正在尝试为下面的代码编写一个Java单元测试
公共类PrintnetRequestTranslator扩展请求翻译器〈MutationOverviewDocRequest,请求内容〉{
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);
}
}
}
下面是RequestContent类
@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;
}
}
下面是我的单元测试代码
类打印网络请求翻译器测试{
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()));
}
}
当我尝试强制转换Delivery类时,出现以下错误。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')
如何解决这个错误/为上述类编写单元测试?
1条答案
按热度按时间mlnl4t2r1#
我遇到了同样的异常,并且注意到,我的解组器在列表中留下了ElementNSIMpl,而不是我自己的类。
这篇文章的第二个答案Intermittent ClassCastException from ElementNSImpl to own type during unmarshalling为我解决了这个问题。