I have the following code which needs the UnsupportedEncodingException
to be handled by try-catch or throws declaration but I want to use neither of them.
@Test
public void serializeAndDeserializeUTF8StringValueExpectingEqual() {
String stringValue = "\u0048\u0065\u006C\u006C\u006F";
String deserializedStringValue = serialzeAndDeserializeStringValue(stringValue);
assertThat(deserializedStringValue.getBytes("UTF-8")).isEqualTo(stringValue.getBytes("UTF-8"));
}
For example I avoided NullPointerException
by using assertThatNullPointerException().isThrownBy
as following
@Test
public void serializeAndDeserializeNullStringValueExpectingEqual() {
String stringValue = null;
assertThatNullPointerException()
.isThrownBy(() -> OutputStreamUtil.serializeString(stringValue, oStream));
}
Is there any way to avoid using try-catch or throws declaration for UnsupportedEncodingException
Maybe should try like this:
@Test
public void serializeAndDeserializeUTF8StringValueExpectingEqual() {
String stringValue = "\u0048\u0065\u006C\u006C\u006F";
String deserializedStringValue = new String(stringValue);
Throwable thrown = catchThrowable(() -> deserializedStringValue.getBytes("UTF-8"));
assertThat(thrown).isExactlyInstanceOf(UnsupportedEncodingException.class);
}