Improve MediaTypeAssert to-string-equality assertions

This commit introduces a `isNotEqualTo(String)` assertion in order to
avoid false negatives when using the default Object-based assertion.

This is a risk since MediaTypeAssert has a `isEqualTo(String)` method
that overrides the base object method and parses the provided String
into a MediaType. Users may thus be tempted to use the reverse assertion
and expect the same parsing behaviour.

This commit also adds tests around the String parsing and the isNotEqual
cases.

Closes gh-32756
This commit is contained in:
Simon Baslé
2024-05-15 11:01:30 +02:00
parent 60c5f44e1d
commit 531da015e1
2 changed files with 78 additions and 7 deletions

View File

@@ -49,10 +49,21 @@ public class MediaTypeAssert extends AbstractObjectAssert<MediaTypeAssert, Media
/**
* Verify that the actual media type is equal to the given string
* representation.
* @param expected the expected media type
* @param mediaType the expected media type, as a String to be parsed
* into a MediaType
*/
public MediaTypeAssert isEqualTo(String mediaType) {
return isEqualTo(parseMediaType(mediaType));
}
/**
* Verify that the actual media type is not equal to the given string
* representation.
* @param mediaType the given media type, as a String to be parsed
* into a MediaType
*/
public MediaTypeAssert isEqualTo(String expected) {
return isEqualTo(parseMediaType(expected));
public MediaTypeAssert isNotEqualTo(String mediaType) {
return isNotEqualTo(parseMediaType(mediaType));
}
/**
@@ -84,7 +95,8 @@ public class MediaTypeAssert extends AbstractObjectAssert<MediaTypeAssert, Media
* // Check that actual is compatible with "text/plain"
* assertThat(mediaType).isCompatibleWith("text/plain");
* </code></pre>
* @param mediaType the media type with which to compare
* @param mediaType the media type with which to compare, as a String
* to be parsed into a MediaType
*/
public MediaTypeAssert isCompatibleWith(String mediaType) {
return isCompatibleWith(parseMediaType(mediaType));