Assertj-core: Add assertion for ByteBuffer

Created on 14 Mar 2019  Â·  2Comments  Â·  Source: assertj/assertj-core

Summary

Code that interacts with ByteBuffer objects could benefit a lot from an improved and dedicated ByteBuffer assertion. Typical assertions are:

  • hasLength
  • isEmpty
  • contains/startsWith/endsWith/equals
    ** String (with CharSet defaulting), byte[], ByteBuffer variants

The assertion could also benefit from a dedicated formatter that represents the buffer in a human-readable format. Currently, we get something like:

expected value: java.nio.HeapByteBuffer[pos=0 lim=7 cap=7]; actual value: java.nio.HeapByteBuffer[pos=0 lim=7 cap=7]

And this allows no diagnosis (except when using a debugger) what happened.

Example

ByteBuffer buf = …
assertThat(buf).equalsTo("some-string");
assertThat(buf).equalsTo("some-string", Charset.defaultCharset());

assertThat(buf).contains("some-string");
assertThat(buf).contains("some-string", Charset.defaultCharset());

assertThat(buf).starts/endsWith("some-string");
new feature

Most helpful comment

If possible I would like to take a stab at it!

All 2 comments

That would be valuable, I agree!

If possible I would like to take a stab at it!

Was this page helpful?
0 / 5 - 0 ratings