Avoid issues with system line separator in tests

See f10caf6aa6
This commit is contained in:
Sam Brannen 2024-06-05 12:16:12 +02:00
parent f4f89aa2a4
commit 9e1ef83669
5 changed files with 18 additions and 22 deletions

View File

@ -251,7 +251,7 @@ public abstract class AbstractEncoderTests<E extends Encoder<?>> extends Abstrac
return dataBuffer -> {
String actual = dataBuffer.toString(UTF_8);
release(dataBuffer);
assertThat(actual).isEqualTo(expected);
assertThat(actual).isEqualToNormalizingNewlines(expected);
};
}

View File

@ -62,12 +62,10 @@ class MediaTypeAssertTests {
@Test
void isEqualInvalidStringShouldFail() {
String ls = System.lineSeparator(); // output below is different between Unix and Windows
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> assertThat(mediaType("application/json")).isEqualTo("example of a bad value"))
.withMessageContaining("[Media type]")
.withMessageEndingWith("To be a valid media type but got:" + ls +
" \"Invalid mime type \"example of a bad value\": does not contain '/'\"" + ls);
.withMessageContainingAll("[Media type]", "To be a valid media type but got:",
"\"Invalid mime type \"example of a bad value\": does not contain '/'\"");
}
@Test
@ -108,12 +106,10 @@ class MediaTypeAssertTests {
@Test
void isNotEqualInvalidStringShouldFail() {
String ls = System.lineSeparator(); // output below is different between Unix and Windows
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> assertThat(mediaType("application/json")).isNotEqualTo("example of a bad value"))
.withMessageContaining("[Media type]")
.withMessageEndingWith("To be a valid media type but got:" + ls +
" \"Invalid mime type \"example of a bad value\": does not contain '/'\"" + ls);
.withMessageContainingAll("[Media type]", "To be a valid media type but got:",
"\"Invalid mime type \"example of a bad value\": does not contain '/'\"");
}
@Test
@ -169,7 +165,7 @@ class MediaTypeAssertTests {
void isCompatibleWithStringAndEmptyExpected() {
assertThatExceptionOfType(AssertionError.class)
.isThrownBy(() -> assertThat(mediaType("application/json")).isCompatibleWith(""))
.withMessageContainingAll("Expecting:", "", "To be a valid media type but got:",
.withMessageContainingAll("Expecting:", "To be a valid media type but got:",
"'mimeType' must not be empty");
}

View File

@ -262,9 +262,11 @@ class Jackson2JsonEncoderTests extends AbstractEncoderTests<Jackson2JsonEncoder>
ObjectMapper mapper = new ObjectMapper().configure(SerializationFeature.INDENT_OUTPUT, true);
this.encoder.registerObjectMappersForType(JacksonViewBean.class, map -> map.put(halMediaType, mapper));
String ls = System.lineSeparator(); // output below is different between Unix and Windows
testEncode(Mono.just(jacksonValue), type, halMediaType, Collections.emptyMap(), step -> step
.consumeNextWith(expectString("{" + ls + " \"withView1\" : \"with\"" + ls + "}"))
.consumeNextWith(expectString("""
{
\s "withView1" : "with"
}"""))
.verifyComplete()
);
}

View File

@ -63,8 +63,6 @@ import static org.assertj.core.api.Assertions.within;
*/
class MappingJackson2HttpMessageConverterTests {
protected static final String NEWLINE_SYSTEM_PROPERTY = System.lineSeparator();
private final MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
@ -369,8 +367,10 @@ class MappingJackson2HttpMessageConverterTests {
this.converter.writeInternal(bean, null, outputMessage);
String result = outputMessage.getBodyAsString(StandardCharsets.UTF_8);
assertThat(result).isEqualTo(("{" + NEWLINE_SYSTEM_PROPERTY +
" \"name\" : \"Jason\"" + NEWLINE_SYSTEM_PROPERTY + "}"));
assertThat(result).isEqualToNormalizingNewlines("""
{
\s "name" : "Jason"
}""");
}
@Test

View File

@ -81,8 +81,6 @@ import static org.springframework.web.testfixture.method.ResolvableMethod.on;
*/
class ResponseEntityResultHandlerTests {
private static final String NEWLINE_SYSTEM_PROPERTY = System.lineSeparator();
private final ResponseEntityResultHandler resultHandler = createHandler();
@ -458,10 +456,10 @@ class ResponseEntityResultHandlerTests {
handler.handleResult(exchange, result).block();
assertThat(exchange.getResponse().getHeaders().getContentType()).isEqualTo(halMediaType);
assertThat(exchange.getResponse().getBodyAsString().block()).isEqualTo(
"{" + NEWLINE_SYSTEM_PROPERTY +
" \"name\" : \"Jason\"" + NEWLINE_SYSTEM_PROPERTY +
"}");
assertThat(exchange.getResponse().getBodyAsString().block()).isEqualToNormalizingNewlines("""
{
\s "name" : "Jason"
}""");
}
@Test // gh-24539