Migrate Hamcrest assertions to AssertJ

Migrate all existing `assertThat(..., Matcher)` assertions to AssertJ
and add checkstyle rules to ensure they don't return.

See gh-23022
This commit is contained in:
Phillip Webb
2019-05-23 15:48:03 -07:00
parent 2294625cf0
commit 95a9d46a87
322 changed files with 4358 additions and 4814 deletions

View File

@@ -34,11 +34,11 @@ import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
import org.xmlunit.matchers.CompareMatcher;
import org.springframework.tests.XmlContent;
import org.springframework.util.xml.StaxUtils;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertTrue;
/**
@@ -85,7 +85,7 @@ public abstract class AbstractMarshallerTests<M extends Marshaller> {
flightElement.appendChild(numberElement);
Text text = expected.createTextNode("42");
numberElement.appendChild(text);
assertThat("Marshaller writes invalid DOMResult", result, isSimilarTo(expected));
assertThat(XmlContent.of(result)).isSimilarToIgnoringWhitespace(expected);
}
@Test
@@ -109,7 +109,7 @@ public abstract class AbstractMarshallerTests<M extends Marshaller> {
flightElement.appendChild(numberElement);
Text text = expected.createTextNode("42");
numberElement.appendChild(text);
assertThat("Marshaller writes invalid DOMResult", result, isSimilarTo(expected));
assertThat(XmlContent.of(result)).isSimilarToIgnoringWhitespace(expected);
}
@Test
@@ -117,7 +117,7 @@ public abstract class AbstractMarshallerTests<M extends Marshaller> {
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
marshaller.marshal(flights, result);
assertThat("Marshaller writes invalid StreamResult", writer.toString(), isSimilarTo(EXPECTED_STRING));
assertThat(XmlContent.of(writer)).isSimilarToIgnoringWhitespace(EXPECTED_STRING);
}
@Test
@@ -125,8 +125,7 @@ public abstract class AbstractMarshallerTests<M extends Marshaller> {
ByteArrayOutputStream os = new ByteArrayOutputStream();
StreamResult result = new StreamResult(os);
marshaller.marshal(flights, result);
assertThat("Marshaller writes invalid StreamResult", new String(os.toByteArray(), "UTF-8"),
isSimilarTo(EXPECTED_STRING));
assertThat(XmlContent.of(new String(os.toByteArray(), "UTF-8"))).isSimilarToIgnoringWhitespace(EXPECTED_STRING);
}
@Test
@@ -136,7 +135,7 @@ public abstract class AbstractMarshallerTests<M extends Marshaller> {
XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(writer);
Result result = StaxUtils.createStaxResult(streamWriter);
marshaller.marshal(flights, result);
assertThat("Marshaller writes invalid StreamResult", writer.toString(), isSimilarTo(EXPECTED_STRING));
assertThat(XmlContent.from(writer)).isSimilarToIgnoringWhitespace(EXPECTED_STRING);
}
@Test
@@ -146,7 +145,7 @@ public abstract class AbstractMarshallerTests<M extends Marshaller> {
XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(writer);
Result result = StaxUtils.createStaxResult(eventWriter);
marshaller.marshal(flights, result);
assertThat("Marshaller writes invalid StreamResult", writer.toString(), isSimilarTo(EXPECTED_STRING));
assertThat(XmlContent.from(writer)).isSimilarToIgnoringWhitespace(EXPECTED_STRING);
}
@Test
@@ -156,7 +155,7 @@ public abstract class AbstractMarshallerTests<M extends Marshaller> {
XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(writer);
StAXResult result = new StAXResult(streamWriter);
marshaller.marshal(flights, result);
assertThat("Marshaller writes invalid StreamResult", writer.toString(), isSimilarTo(EXPECTED_STRING));
assertThat(XmlContent.from(writer)).isSimilarToIgnoringWhitespace(EXPECTED_STRING);
}
@Test
@@ -166,11 +165,6 @@ public abstract class AbstractMarshallerTests<M extends Marshaller> {
XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(writer);
StAXResult result = new StAXResult(eventWriter);
marshaller.marshal(flights, result);
assertThat("Marshaller writes invalid StreamResult", writer.toString(), isSimilarTo(EXPECTED_STRING));
}
private static CompareMatcher isSimilarTo(final Object content) {
return CompareMatcher.isSimilarTo(content)
.ignoreWhitespace();
assertThat(XmlContent.from(writer)).isSimilarToIgnoringWhitespace(EXPECTED_STRING);
}
}

View File

@@ -52,12 +52,13 @@ import org.springframework.oxm.jaxb.test.FlightType;
import org.springframework.oxm.jaxb.test.Flights;
import org.springframework.oxm.jaxb.test.ObjectFactory;
import org.springframework.oxm.mime.MimeContainer;
import org.springframework.tests.XmlContent;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.ReflectionUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@@ -73,7 +74,6 @@ import static org.xmlunit.diff.ComparisonType.XML_STANDALONE;
import static org.xmlunit.diff.DifferenceEvaluators.Default;
import static org.xmlunit.diff.DifferenceEvaluators.chain;
import static org.xmlunit.diff.DifferenceEvaluators.downgradeDifferencesToEqual;
import static org.xmlunit.matchers.CompareMatcher.isSimilarTo;
/**
* @author Arjen Poutsma
@@ -135,8 +135,7 @@ public class Jaxb2MarshallerTests extends AbstractMarshallerTests<Jaxb2Marshalle
StreamResult result = new StreamResult(writer);
marshaller.marshal(flights, result);
DifferenceEvaluator ev = chain(Default, downgradeDifferencesToEqual(XML_STANDALONE));
assertThat("Marshaller writes invalid StreamResult", writer.toString(),
isSimilarTo(EXPECTED_STRING).withDifferenceEvaluator(ev));
assertThat(XmlContent.from(writer)).isSimilarTo(EXPECTED_STRING, ev);
}
@Test
@@ -322,9 +321,7 @@ public class Jaxb2MarshallerTests extends AbstractMarshallerTests<Jaxb2Marshalle
Result result = new StreamResult(writer);
marshaller.marshal(airplane, result);
DifferenceEvaluator ev = chain(Default, downgradeDifferencesToEqual(XML_STANDALONE));
assertThat("Marshalling should use root Element",
writer.toString(),
isSimilarTo("<airplane><name>test</name></airplane>").withDifferenceEvaluator(ev));
assertThat(XmlContent.from(writer)).isSimilarTo("<airplane><name>test</name></airplane>", ev);
}
@Test // SPR-10806

View File

@@ -23,13 +23,13 @@ import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.oxm.AbstractMarshallerTests;
import org.springframework.tests.XmlContent;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue;
import static org.xmlunit.matchers.CompareMatcher.isSimilarTo;
/**
* NOTE: These tests fail under Eclipse/IDEA because JiBX binding does not occur by
@@ -81,7 +81,7 @@ public class JibxMarshallerTests extends AbstractMarshallerTests<JibxMarshaller>
String expected =
"<?xml version=\"1.0\"?>\n" + "<flights xmlns=\"http://samples.springframework.org/flight\">\n" +
" <flight>\n" + " <number>42</number>\n" + " </flight>\n" + "</flights>";
assertThat(writer.toString(), isSimilarTo(expected).ignoreWhitespace());
assertThat(XmlContent.from(writer)).isSimilarToIgnoringWhitespace(expected);
}
@Test

View File

@@ -56,9 +56,10 @@ import org.xml.sax.ContentHandler;
import org.xmlunit.builder.Input;
import org.xmlunit.xpath.JAXPXPathEngine;
import org.springframework.tests.XmlContent;
import org.springframework.util.xml.StaxUtils;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
@@ -66,7 +67,6 @@ import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isA;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.xmlunit.matchers.CompareMatcher.isSimilarTo;
/**
* @author Arjen Poutsma
@@ -106,7 +106,7 @@ public class XStreamMarshallerTests {
flightElement.appendChild(numberElement);
Text text = expected.createTextNode("42");
numberElement.appendChild(text);
assertThat("Marshaller writes invalid DOMResult", document, isSimilarTo(expected));
assertThat(XmlContent.of(document)).isSimilarTo(expected);
}
// see SWS-392
@@ -135,7 +135,7 @@ public class XStreamMarshallerTests {
eFlightElement.appendChild(eNumberElement);
Text text = expected.createTextNode("42");
eNumberElement.appendChild(text);
assertThat("Marshaller writes invalid DOMResult", existent, isSimilarTo(expected));
assertThat(XmlContent.of(existent)).isSimilarTo(expected);
}
@Test
@@ -143,7 +143,7 @@ public class XStreamMarshallerTests {
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
marshaller.marshal(flight, result);
assertThat("Marshaller writes invalid StreamResult", writer.toString(), isSimilarTo(EXPECTED_STRING));
assertThat(XmlContent.from(writer)).isSimilarTo(EXPECTED_STRING);
}
@Test
@@ -152,7 +152,7 @@ public class XStreamMarshallerTests {
StreamResult result = new StreamResult(os);
marshaller.marshal(flight, result);
String s = new String(os.toByteArray(), "UTF-8");
assertThat("Marshaller writes invalid StreamResult", s, isSimilarTo(EXPECTED_STRING));
assertThat(XmlContent.of(s)).isSimilarTo(EXPECTED_STRING);
}
@Test
@@ -177,7 +177,7 @@ public class XStreamMarshallerTests {
XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(writer);
Result result = StaxUtils.createStaxResult(streamWriter);
marshaller.marshal(flight, result);
assertThat("Marshaller writes invalid StreamResult", writer.toString(), isSimilarTo(EXPECTED_STRING));
assertThat(XmlContent.from(writer)).isSimilarTo(EXPECTED_STRING);
}
@Test
@@ -187,7 +187,7 @@ public class XStreamMarshallerTests {
XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(writer);
Result result = StaxUtils.createStaxResult(eventWriter);
marshaller.marshal(flight, result);
assertThat("Marshaller writes invalid StreamResult", writer.toString(), isSimilarTo(EXPECTED_STRING));
assertThat(XmlContent.from(writer)).isSimilarTo(EXPECTED_STRING);
}
@Test
@@ -196,7 +196,7 @@ public class XStreamMarshallerTests {
byte[] buf = new byte[]{0x1, 0x2};
Writer writer = new StringWriter();
marshaller.marshal(buf, new StreamResult(writer));
assertThat(writer.toString(), isSimilarTo("<byte-array>AQI=</byte-array>"));
assertThat(XmlContent.from(writer)).isSimilarTo("<byte-array>AQI=</byte-array>");
Reader reader = new StringReader(writer.toString());
byte[] bufResult = (byte[]) marshaller.unmarshal(new StreamSource(reader));
assertTrue("Invalid result", Arrays.equals(buf, bufResult));
@@ -208,7 +208,7 @@ public class XStreamMarshallerTests {
Writer writer = new StringWriter();
marshaller.marshal(flight, new StreamResult(writer));
String expected = "<flight flightNumber=\"42\" />";
assertThat("Marshaller does not use attributes", writer.toString(), isSimilarTo(expected));
assertThat(XmlContent.from(writer)).isSimilarTo(expected);
}
@Test
@@ -217,7 +217,7 @@ public class XStreamMarshallerTests {
Writer writer = new StringWriter();
marshaller.marshal(flight, new StreamResult(writer));
String expected = "<flight flightNumber=\"42\" />";
assertThat("Marshaller does not use attributes", writer.toString(), isSimilarTo(expected));
assertThat(XmlContent.from(writer)).isSimilarTo(expected);
}
@Test
@@ -226,7 +226,7 @@ public class XStreamMarshallerTests {
Writer writer = new StringWriter();
marshaller.marshal(flight, new StreamResult(writer));
String expected = "<flight flightNumber=\"42\" />";
assertThat("Marshaller does not use attributes", writer.toString(), isSimilarTo(expected));
assertThat(XmlContent.from(writer)).isSimilarTo(expected);
}
@Test
@@ -235,7 +235,7 @@ public class XStreamMarshallerTests {
Writer writer = new StringWriter();
marshaller.marshal(flight, new StreamResult(writer));
String expected = "<flight flightNumber=\"42\" />";
assertThat("Marshaller does not use attributes", writer.toString(), isSimilarTo(expected));
assertThat(XmlContent.from(writer)).isSimilarTo(expected);
}
@Test
@@ -249,7 +249,7 @@ public class XStreamMarshallerTests {
Writer writer = new StringWriter();
marshaller.marshal(flight, new StreamResult(writer));
assertThat("Marshaller does not use attributes", writer.toString(), isSimilarTo(EXPECTED_STRING));
assertThat(XmlContent.from(writer)).isSimilarTo(EXPECTED_STRING);
}
@Test
@@ -263,7 +263,7 @@ public class XStreamMarshallerTests {
Writer writer = new StringWriter();
marshaller.marshal(flight, new StreamResult(writer));
assertThat("Marshaller does not use attributes", writer.toString(), isSimilarTo(EXPECTED_STRING));
assertThat(XmlContent.from(writer)).isSimilarTo(EXPECTED_STRING);
}
@Test
@@ -272,7 +272,7 @@ public class XStreamMarshallerTests {
Writer writer = new StringWriter();
marshaller.marshal(flight, new StreamResult(writer));
String expected = "<flight><flightNo>42</flightNo></flight>";
assertThat("Marshaller does not use aliases", writer.toString(), isSimilarTo(expected));
assertThat(XmlContent.from(writer)).isSimilarTo(expected);
}
@Test
@@ -347,7 +347,7 @@ public class XStreamMarshallerTests {
flight.setFlightNumber(42);
marshaller.marshal(flight, result);
String expected = "<flight><number>42</number></flight>";
assertThat("Marshaller writes invalid StreamResult", writer.toString(), isSimilarTo(expected));
assertThat(XmlContent.from(writer)).isSimilarTo(expected);
}