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.
Describe the bug
The method
AbstractInputStreamAssert#hasSameContentAs(InputStream)is implemented by converting the bytes to aStringand 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.
Test case reproducing the bug
The assertion erroneously passes because the single byte
-128is invalid for UTF-8 and is replaced by the replacement character (whose UTF-8 bytes are-17, -65, -67).Suggested fix
Maybe deprecate
hasSameContentAsand instead addhasSameBinaryContentAs&hasSameTextualContentAs, similar to how it was done for the asserts forFileandPath, see 2f44bab.