I'm trying to test some code that uses scodec.bits.ByteVector
.
In particular I'm using ByteVector.encodeUtf8(str: String): Either[CharacterCodingException, ByteVector]
Since this can return an potential error if encoding to UTF-8 fails, I have to handle the error condition. Of course, I can hide the call and mock my trait so that I forcibly return a Left[CharacterEncodingException]
but that's too onerous.
What I would love to do is to create a String
that has some invalid utf-8 bytes and call encodeUtf8
with that.
My guess is that this is not possible. No matter what I do, String class will coerce any bad entries into something that is nonsensical but is still valid UTF-8 (e.g. �). Is this right?
This is how I've been trying to create such a string:
new String(Array(255.toByte), "utf-8")
I also tried to create a string in some other encodings and then use that to encode to UTF-8 but ByteVector handles it.
Is this possible?