Validate ISO 20022 XML Against an XSD in REST Assured
Validate ISO 20022 payment files and status reports against XSDs with matchesXsdInClasspath(..), check the test data a test sends, test an accepted and a rejected file, and handle the SAXParseException an invalid document throws.
A company sends its monthly salary run to the bank as an ISO 20022 XML file, and the bank answers with a status report. Your test for that API sends a hand-edited file, and a rejection test passes, but only because the file was missing a required attribute, not for the reason the test was about. In this guide, you’ll validate both directions against their XML Schemas: the payment files your tests send, and the status reports the API returns, accepted or rejected.
Level: Intermediate · Time: 20 minutes · Part of: Module 6: Schema and Contract Validation, section 3
What You’ll Build
You’ll build a test class called XmlSchemaTest with four tests. One validates the test file itself, two send it and validate the API’s accepted and rejected reports, and one proves the XSD rejects a broken file.
The accepted-file test sends this request and validates this response:
1
2
3
4
5
6
7
8
9
10
POST /pisp/v1/payment-files HTTP/1.1
Host: localhost:<port>
Content-Type: application/xml
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.09">...</Document>
HTTP/1.1 202 Accepted
Content-type: application/xml; charset=utf-8
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.002.001.10">...<GrpSts>ACCP</GrpSts>...</Document>
When you’re done, Maven reports four passing tests:
1
2
[INFO] Tests run: 4, Failures: 0, Errors: 0, Skipped: 0
[INFO] BUILD SUCCESS
In this guide, you will:
- Validate the test data you send.
- Validate an accepted report.
- Validate a rejected report.
- Test a document the XSD rejects.
What You Need
- About 20 minutes
- JDK 26 or later
- Maven 3.9 or later
- An IDE (IntelliJ IDEA, VS Code or Eclipse)
- Knowledge: you can read XML with GPath, as taught in Check XML and HTML Responses in REST Assured, and validate JSON against a schema, from Validate Responses Against a JSON Schema in REST Assured.
How to Complete This Guide
You can work through this guide in one of two ways:
- Build it step by step: create
XmlSchemaTestin a newsection_03package next to the classes from the previous sections, and follow every heading below. - Skip ahead: open the finished class (
XmlSchemaTest.java) and read along. You can run it at any time with the command in Run the Tests.
Get the code: if you haven’t cloned the examples repository yet, follow the steps in Validate Responses Against a JSON Schema in REST Assured.
Out of scope: this guide doesn’t cover the full official ISO 20022 schemas, or XML namespaces in paths; the module uses simplified XSDs, and namespaces are taught in Check XML and HTML Responses in REST Assured.
Understand XML Schema Validation
The problem. ISO 20022 messages have official XML Schemas, and a bank rejects a file that doesn’t match before anyone reads it. A test suite for such an API has two risks: a report from the API that drifts from its schema, and a test file that’s invalid to begin with, so tests fail or pass for reasons that have nothing to do with what they test.
The concept. An XML Schema Definition (XSD) describes the elements an XML document may contain, in which order, how often, with which attributes and with which types. REST Assured validates against one with two matchers:
matchesXsdInClasspath(path)loads the XSD from the classpath.matchesXsd(xsd)takes it as a string, aFileor anInputStream.
Both are static imports from io.restassured.matcher.RestAssuredMatchers. Like the JSON Schema matcher, they’re Hamcrest matchers on a String, so they work on responses and on any other XML.
When to use it. Validate every XML response against its XSD, and validate the XML files your tests send against theirs. Combine the XSD with path checks, because a schema checks a message’s shape, not whether its content is right.
Validate what you send and what you receive, and check the meaning with paths.
Meet the API Under Test
Two ISO 20022 message types are involved:
| Message | Name | Direction |
|---|---|---|
pain.001 | Customer Credit Transfer Initiation | The company sends payments to the bank |
pain.002 | Customer Payment Status Report | The bank reports whether it accepted them |
POST /pisp/v1/payment-files takes a pain.001 file and answers 202 Accepted with a pain.002 report. The test file pays two salaries:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.09">
<CstmrCdtTrfInitn>
<GrpHdr>
<MsgId>SAL-2026-03</MsgId>
<CreDtTm>2026-03-25T09:00:00</CreDtTm>
<NbOfTxs>2</NbOfTxs>
</GrpHdr>
<PmtInf>
<PmtInfId>SAL-2026-03-1</PmtInfId>
<DbtrAcct><Id><IBAN>NL91ABNA0417164300</IBAN></Id></DbtrAcct>
<CdtTrfTxInf>
<EndToEndId>SAL-JAN-JANSEN</EndToEndId>
<InstdAmt Ccy="EUR">2450.00</InstdAmt>
<Cdtr><Nm>Jan Jansen</Nm></Cdtr>
<CdtrAcct><Id><IBAN>NL20INGB0001234567</IBAN></Id></CdtrAcct>
</CdtTrfTxInf>
<CdtTrfTxInf>
<EndToEndId>SAL-ANNA-DE-VRIES</EndToEndId>
<InstdAmt Ccy="EUR">2780.00</InstdAmt>
<Cdtr><Nm>Anna de Vries</Nm></Cdtr>
<CdtrAcct><Id><IBAN>NL69INGB0123456789</IBAN></Id></CdtrAcct>
</CdtTrfTxInf>
</PmtInf>
</CstmrCdtTrfInitn>
</Document>
The API accepts the file (ACCP) when NbOfTxs matches the number of transactions, and rejects it (RJCT) when it doesn’t. A rejected report looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.002.001.10">
<CstmrPmtStsRpt>
<GrpHdr>
<MsgId>STS-SAL-2026-03</MsgId>
<CreDtTm>2026-11-21T20:00:55</CreDtTm>
</GrpHdr>
<OrgnlGrpInfAndSts>
<OrgnlMsgId>SAL-2026-03</OrgnlMsgId>
<OrgnlMsgNmId>pain.001.001.09</OrgnlMsgNmId>
<OrgnlNbOfTxs>3</OrgnlNbOfTxs>
<GrpSts>RJCT</GrpSts>
<StsRsnInf>
<AddtlInf>NbOfTxs is 3 but the file has 2 transactions</AddtlInf>
</StsRsnInf>
</OrgnlGrpInfAndSts>
</CstmrPmtStsRpt>
</Document>
An accepted report has the same shape, with ACCP and no StsRsnInf. The XSDs in src/test/resources/xsd are simplified subsets of the official ones: they keep the element names, nesting and namespaces of the standard, and leave out most optional elements. The pain.001 schema, for example, requires a Ccy attribute of three capital letters on every InstdAmt.
Your tests check these values:
- The whole structure of the file and of each report, against their XSDs.
OrgnlMsgIdandGrpSts, which say which file the report refers to and whether it was accepted.StsRsnInf.AddtlInf, the reason for a rejection.
Validate the Test Data You Send
Set Up the Test Class
Goal: create the test class, start the API and read the salary file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.javanibble.module_06.section_03;
import static io.restassured.RestAssured.given;
import static io.restassured.matcher.RestAssuredMatchers.matchesXsdInClasspath;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.jupiter.api.Assertions.assertThrows;
import com.javanibble.pisp.PaymentInitiationApi;
import io.restassured.http.ContentType;
import io.restassured.specification.RequestSpecification;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.xml.sax.SAXParseException;
class XmlSchemaTest {
private static final String PAIN_001_XSD = "xsd/pain.001.001.09-simplified.xsd";
private static final String PAIN_002_XSD = "xsd/pain.002.001.10-simplified.xsd";
private static final String STATUS = "Document.CstmrPmtStsRpt.OrgnlGrpInfAndSts";
private static PaymentInitiationApi api;
private static String salaries;
@BeforeAll
static void startApi() throws IOException {
api = PaymentInitiationApi.start();
try (InputStream in = XmlSchemaTest.class.getResourceAsStream("/pain/salaries-march.xml")) {
salaries = new String(in.readAllBytes(), StandardCharsets.UTF_8);
}
}
@AfterAll
static void stopApi() {
api.close();
}
private static RequestSpecification paymentFiles() {
return given().baseUri(api.baseUri()).port(api.port()).basePath(PaymentInitiationApi.BASE_PATH)
.contentType(ContentType.XML);
}
}
What’s happening:
PAIN_001_XSDandPAIN_002_XSDare the two schemas, andSTATUSis the long path to the report’s group status.@BeforeAllreadssalaries-march.xmlfrom the classpath into a string.paymentFiles()sends XML, the content type the endpoint expects.
Checkpoint: the class compiles. There are no tests yet, so there’s nothing to run until the next step.
Check the Salary File
Goal: prove that the file the tests send is valid.
1
2
3
4
@Test
void theTestDataIsValidToo() {
assertThat(salaries, matchesXsdInClasspath(PAIN_001_XSD));
}
What’s happening:
assertThat(..)is Hamcrest’s, and the XSD matcher works on any string, so no request is sent.- If the test file were invalid, every test that sends it would test the wrong thing: the API might reject it for a reason unrelated to the test. One line rules that out.
Checkpoint: run XmlSchemaTest from your IDE. One test runs and passes.
Validate an Accepted Report
Send the File and Check the Report
Goal: send the salary file, and check the report’s structure and meaning.
1
2
3
4
5
6
7
8
9
10
11
12
13
@Test
void anAcceptedFile() {
paymentFiles()
.body(salaries)
.when()
.post("/payment-files")
.then()
.statusCode(202)
.contentType(ContentType.XML)
.body(matchesXsdInClasspath(PAIN_002_XSD))
.body(STATUS + ".OrgnlMsgId", equalTo("SAL-2026-03"))
.body(STATUS + ".GrpSts", equalTo("ACCP"));
}
What’s happening:
body(matchesXsdInClasspath(PAIN_002_XSD))checks the structure of the report against thepain.002schema.- The path checks, with XML GPath, check its meaning: the report refers to the file that was sent, and accepts it.
ACCPis the ISO 20022 code for “accepted”.
Checkpoint: run the class again. Two tests pass.
Use the XSD for the message you’re checking, in the right version. An XSD validates elements in its target namespace only, so checking a
pain.001against thepain.002schema, or apain.001.001.03file against the001.09schema, fails withcvc-elt.1.a: Cannot find the declaration of element 'Document'., which says nothing about versions. Check thexmlnsfirst.
Validate a Rejected Report
Send a File With the Wrong Count
Goal: prove that a rejection is still a valid report, with the reason in it.
1
2
3
4
5
6
7
8
9
10
11
12
@Test
void aRejectedFileIsStillAValidReport() {
paymentFiles()
.body(salaries.replace("<NbOfTxs>2</NbOfTxs>", "<NbOfTxs>3</NbOfTxs>"))
.when()
.post("/payment-files")
.then()
.statusCode(202)
.body(matchesXsdInClasspath(PAIN_002_XSD))
.body(STATUS + ".GrpSts", equalTo("RJCT"))
.body(STATUS + ".StsRsnInf.AddtlInf", containsString("the file has 2 transactions"));
}
What’s happening:
- The file declares three transactions but has two, so the API rejects it.
- A rejection is a business outcome, not an HTTP error: the status is still
202, and the reason is in the report. - The schema must hold for rejections too, including the optional
StsRsnInfelement that only appears in them.
Checkpoint: run the class again. Three tests pass.
Don’t treat the XSD as the whole contract. It checks structure, not meaning:
NbOfTxscan be a valid positive number and still be wrong. That’s why these tests checkGrpStsand the reason with paths as well.
Test a Document the XSD Rejects
Remove a Required Attribute
Goal: prove that the XSD rejects a file without a currency on an amount, and for that reason.
1
2
3
4
5
6
7
8
@Test
void theXsdRejectsBrokenDocuments() {
String noAmountCurrency = salaries.replace("<InstdAmt Ccy=\"EUR\">2450.00</InstdAmt>", "<InstdAmt>2450.00</InstdAmt>");
SAXParseException invalid = assertThrows(SAXParseException.class,
() -> assertThat(noAmountCurrency, matchesXsdInClasspath(PAIN_001_XSD)));
assertThat(invalid.getMessage(), containsString("Attribute 'Ccy' must appear on element 'InstdAmt'"));
}
What’s happening:
- The first transaction loses its
Ccyattribute, which thepain.001schema requires. - An invalid document doesn’t make the XSD matcher return
false. It rethrows the validator’sorg.xml.sax.SAXParseException, with a message such ascvc-complex-type.4: Attribute 'Ccy' must appear on element 'InstdAmt'. - So the test expects the exception with
assertThrows(..), and checks its message to be sure the document failed for the reason it meant.
Checkpoint: run the class again. All four tests pass.
Don’t wrap the XSD matcher in
not(..). This is where it differs from the JSON Schema matcher:not(matchesXsdInClasspath(..))throwsSAXParseExceptionon an invalid document instead of passing, so it can’t express “is invalid”. UseassertThrows(SAXParseException.class, ..).
Run the Tests
Run the following command from the repository root:
1
mvn test -pl module-06 -Dtest=XmlSchemaTest
You should see a result similar to the following:
1
2
3
4
5
6
[INFO] Running com.javanibble.module_06.section_03.XmlSchemaTest
[INFO] Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.444 s -- in com.javanibble.module_06.section_03.XmlSchemaTest
[INFO]
[INFO] Tests run: 4, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] BUILD SUCCESS
Make a Test Fail
In anAcceptedFile, change matchesXsdInClasspath(PAIN_002_XSD) to matchesXsdInClasspath(PAIN_001_XSD) and run the command again. You should see a result similar to the following:
1
2
[ERROR] Errors:
[ERROR] XmlSchemaTest.anAcceptedFile:73 » SAXParse cvc-elt.1.a: Cannot find the declaration of element 'Document'.
The report is a valid pain.002, but it was checked against the pain.001 schema, whose Document lives in another namespace. Notice that JUnit reports an error, not a failure: inside a then() chain too, an invalid document throws SAXParseException, not an AssertionError. The test still fails the build.
Change PAIN_001_XSD back to PAIN_002_XSD and run the tests once more to confirm they pass.
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
SAXParse cvc-elt.1.a: Cannot find the declaration of element 'Document'. | The XSD is for another message, or another version of it | Use the XSD whose target namespace matches the document’s xmlns |
cvc-complex-type.4: Attribute 'Ccy' must appear on element 'InstdAmt'. | A required attribute is missing | Add it, or check that the test meant to remove it |
cvc-pattern-valid: Value 'euro' is not facet-valid ... | A value breaks a pattern in the XSD | Use a value that matches, such as EUR |
not(matchesXsdInClasspath(..)) throws instead of passing | The XSD matcher throws on invalid documents | Use assertThrows(SAXParseException.class, ..) |
Try It Yourself
Exercise 1: Reject a Lower-Case Currency
The pain.001 schema requires a currency of three capital letters. Add a test called aLowerCaseCurrencyIsRejected that changes the first amount’s currency to Ccy="euro", and checks that the XSD rejects the file with a message that names the value euro.
Hint
Start from theXsdRejectsBrokenDocuments. The message reads cvc-pattern-valid: Value 'euro' is not facet-valid with respect to pattern '[A-Z]{3}' ....
Solution
1
2
3
4
5
6
7
8
@Test
void aLowerCaseCurrencyIsRejected() {
String lowerCase = salaries.replace("<InstdAmt Ccy=\"EUR\">2450.00</InstdAmt>", "<InstdAmt Ccy=\"euro\">2450.00</InstdAmt>");
SAXParseException invalid = assertThrows(SAXParseException.class,
() -> assertThat(lowerCase, matchesXsdInClasspath(PAIN_001_XSD)));
assertThat(invalid.getMessage(), containsString("euro"));
}
Exercise 2: Send a File With One Salary
Add a test called aFileWithOneSalaryIsAccepted. Build a file from salaries without the second CdtTrfTxInf element and with NbOfTxs set to 1. Check that the new file is valid against the pain.001 schema, then send it, and check that the report is a valid pain.002, accepts the file, and reports 1 as the original number of transactions.
Hint
Find the second <CdtTrfTxInf> with indexOf(..), starting after the first </CdtTrfTxInf>, and cut it out with substring(..). XML values are text, so compare OrgnlNbOfTxs with the string "1".
Solution
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@Test
void aFileWithOneSalaryIsAccepted() {
int start = salaries.indexOf("<CdtTrfTxInf>", salaries.indexOf("</CdtTrfTxInf>"));
int end = salaries.indexOf("</CdtTrfTxInf>", start) + "</CdtTrfTxInf>".length();
String oneSalary = (salaries.substring(0, start) + salaries.substring(end))
.replace("<NbOfTxs>2</NbOfTxs>", "<NbOfTxs>1</NbOfTxs>");
assertThat(oneSalary, matchesXsdInClasspath(PAIN_001_XSD));
paymentFiles()
.body(oneSalary)
.when()
.post("/payment-files")
.then()
.statusCode(202)
.body(matchesXsdInClasspath(PAIN_002_XSD))
.body(STATUS + ".GrpSts", equalTo("ACCP"))
.body(STATUS + ".OrgnlNbOfTxs", equalTo("1"));
}
Summary
You built XmlSchemaTest, which validates the salary file the tests send, the accepted and rejected status reports the API returns, and a broken file the XSD must reject. A rejection test can no longer pass because the test file itself was invalid.
matchesXsdInClasspath(..)validates XML against an XML Schema, for responses and, because it’s a Hamcrest matcher, for any XML a test uses.- For ISO 20022, check both the
pain.001files a test sends and thepain.002reports the API returns, accepted or rejected. - An XSD validates elements in its target namespace only, so the wrong schema or version fails with
Cannot find the declaration of element 'Document'. - An invalid document makes the matcher throw
SAXParseException, also insidethen(); test for invalidity withassertThrows(..), notnot(..). - Combine the XSD with path checks, because a schema checks a message’s shape, not whether its content is right.
Here are the commands you used in this guide:
1
mvn test -pl module-06 -Dtest=XmlSchemaTest
What’s Next
Next: continue with Check Every Response Against OpenAPI With a REST Assured Filter, which checks every response against the API’s OpenAPI 3.1 description.
The code:
- The finished test class:
XmlSchemaTest.java - The XSDs and the test file:
src/test/resources - The code and run commands for this module: Module 6 README
Go deeper:
- ISO 20022 message definitions, for the official
pain.001andpain.002schemas. - W3C XML Schema Part 1: Structures, for how elements, attributes and namespaces are validated.
Related guides:
- Check XML and HTML Responses in REST Assured, for the XML paths these tests combine with the XSD.
- Validate Responses Against a JSON Schema in REST Assured, for the JSON counterpart, which returns
falseinstead of throwing.
