Skip to content

AbstractInputStreamAssert#hasSameContentAs(InputStream) converts bytes to String #4146

Description

@Marcono1234

Describe the bug
The method AbstractInputStreamAssert#hasSameContentAs(InputStream) is implemented by converting the bytes to a String and then performing the assertion.

This is most likely unexpected by most users and can cause spurious successful tests when bytes unsupported by the default charset are present in the streams.

  • assertj core version: v4.0.0-M1
  • java version: JDK 22.0.2+9
  • test framework version: JUnit 6.0.1
  • os (if relevant): Windows 11

Test case reproducing the bug

// Assumes default charset is UTF-8 (the default since JDK 18)
var stream1 = new ByteArrayInputStream(new byte[] {-128});
var stream2 = new ByteArrayInputStream(new byte[] {-17, -65, -67});
assertThat(stream1).hasSameContentAs(stream2);

The assertion erroneously passes because the single byte -128 is invalid for UTF-8 and is replaced by the replacement character (whose UTF-8 bytes are -17, -65, -67).

Suggested fix
Maybe deprecate hasSameContentAs and instead add hasSameBinaryContentAs & hasSameTextualContentAs, similar to how it was done for the asserts for File and Path, see 2f44bab.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions