Browse Source

Polish XmlExpectationsHelper[Tests]

pull/33953/head
Sam Brannen 1 year ago
parent
commit
a3c132c442
  1. 2
      spring-test/src/main/java/org/springframework/test/util/XmlExpectationsHelper.java
  2. 44
      spring-test/src/test/java/org/springframework/test/util/XmlExpectationsHelperTests.java

2
spring-test/src/main/java/org/springframework/test/util/XmlExpectationsHelper.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

44
spring-test/src/test/java/org/springframework/test/util/XmlExpectationsHelperTests.java

@ -27,60 +27,54 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType; @@ -27,60 +27,54 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
*/
class XmlExpectationsHelperTests {
private static final String CONTROL = "<root><field1>f1</field1><field2>f2</field2></root>";
private final XmlExpectationsHelper xmlHelper = new XmlExpectationsHelper();
@Test
void assertXmlEqualForEqual() throws Exception {
String control = "<root><field1>f1</field1><field2>f2</field2></root>";
String test = "<root><field1>f1</field1><field2>f2</field2></root>";
XmlExpectationsHelper xmlHelper = new XmlExpectationsHelper();
xmlHelper.assertXmlEqual(control, test);
xmlHelper.assertXmlEqual(CONTROL, test);
}
@Test
void assertXmlEqualExceptionForIncorrectValue() {
String control = "<root><field1>f1</field1><field2>f2</field2></root>";
String test = "<root><field1>notf1</field1><field2>f2</field2></root>";
XmlExpectationsHelper xmlHelper = new XmlExpectationsHelper();
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
xmlHelper.assertXmlEqual(control, test))
.withMessageStartingWith("Body content Expected child 'field1'");
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> xmlHelper.assertXmlEqual(CONTROL, test))
.withMessageStartingWith("Body content Expected child 'field1'");
}
@Test
void assertXmlEqualForOutOfOrder() throws Exception {
String control = "<root><field1>f1</field1><field2>f2</field2></root>";
String test = "<root><field2>f2</field2><field1>f1</field1></root>";
XmlExpectationsHelper xmlHelper = new XmlExpectationsHelper();
xmlHelper.assertXmlEqual(control, test);
xmlHelper.assertXmlEqual(CONTROL, test);
}
@Test
void assertXmlEqualExceptionForMoreEntries() {
String control = "<root><field1>f1</field1><field2>f2</field2></root>";
String test = "<root><field1>f1</field1><field2>f2</field2><field3>f3</field3></root>";
XmlExpectationsHelper xmlHelper = new XmlExpectationsHelper();
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
xmlHelper.assertXmlEqual(control, test))
.withMessageContaining("Expected child nodelist length '2' but was '3'");
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> xmlHelper.assertXmlEqual(CONTROL, test))
.withMessageContaining("Expected child nodelist length '2' but was '3'");
}
@Test
void assertXmlEqualExceptionForLessEntries() {
void assertXmlEqualExceptionForFewerEntries() {
String control = "<root><field1>f1</field1><field2>f2</field2><field3>f3</field3></root>";
String test = "<root><field1>f1</field1><field2>f2</field2></root>";
XmlExpectationsHelper xmlHelper = new XmlExpectationsHelper();
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
xmlHelper.assertXmlEqual(control, test))
.withMessageContaining("Expected child nodelist length '3' but was '2'");
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> xmlHelper.assertXmlEqual(control, test))
.withMessageContaining("Expected child nodelist length '3' but was '2'");
}
@Test
void assertXmlEqualExceptionWithFullDescription() {
String control = "<root><field1>f1</field1><field2>f2</field2></root>";
String test = "<root><field2>f2</field2><field3>f3</field3></root>";
XmlExpectationsHelper xmlHelper = new XmlExpectationsHelper();
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
xmlHelper.assertXmlEqual(control, test))
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> xmlHelper.assertXmlEqual(CONTROL, test))
.withMessageContaining("Expected child 'field1' but was 'null'")
.withMessageContaining("Expected child 'null' but was 'field3'");
}

Loading…
Cancel
Save