Skip to content

MediaTypeAssert risk false negative with isNotEqual assertion #32756

@simonbasle

Description

@simonbasle

In MediaTypeAssert, we have both isEqualTo(Object) and isEqualTo(String), but only isNotEqualTo(Object).
The later is inherited from AbstractAssert.

As a String is never equal to a MediaType, the following test appears to pass but it does for the wrong reasons (false negative):

@Test
void isNotEqualWhenDifferentShouldPass() {
	assertThat(mediaType("application/json")).isNotEqualTo(MediaType.TEXT_HTML_VALUE);
}

To make the issue more evident, one can try the following test instead, which doesn't pass with the current implementation:

@Test
void isNotEqualWhenSameShouldFail() {
	assertThatExceptionOfType(AssertionError.class)
			.isThrownBy(() -> assertThat(mediaType("application/json")).isNotEqualTo(MediaType.APPLICATION_JSON_VALUE))
			.withMessageContaining("Media type");
}

(note that isNotEqual isn't currently tested in MediaTypeAssertTests, such tests should be added as part of fixing this issue)

Metadata

Metadata

Assignees

Labels

in: testIssues in the test moduletype: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions