### Question:
CoapPacket implements Serializable { public void writeTo(OutputStream outputStream) { try { int tempByte; tempByte = (0x3 & version) << 6; tempByte |= (0x3 & messageType.ordinal()) << 4; tempByte |= token.length & 0xF; outputStream.write(tempByte); writeCode(outputStream, this); outputStream.write(0xFF & (messageId >> 8)); outputStream.write(0xFF & messageId); outputStream.write(token); options.serialize(outputStream); if (payload != null && payload.length > 0) { outputStream.write(PAYLOAD_MARKER); outputStream.write(payload); } } catch (IOException exception) { throw new IllegalStateException(exception.getMessage(), exception); } } CoapPacket(InetSocketAddress remoteAddress);  CoapPacket(Code code, MessageType messageType, InetSocketAddress otherEndAddress);  CoapPacket(Method method, MessageType messageType, String uriPath, InetSocketAddress remoteAddress); static CoapPacket read(InetSocketAddress remoteAddress, byte[] rawData, int length); static CoapPacket read(InetSocketAddress remoteAddress, byte[] rawData); InetSocketAddress getRemoteAddress(); static CoapPacket deserialize(InetSocketAddress remoteAddress, InputStream inputStream); static byte[] serialize(CoapPacket coapPacket); final HeaderOptions headers(); void setHeaderOptions(HeaderOptions options); CoapPacket createResponse(); CoapPacket createResponse(Code responseCode); byte getVersion(); MessageType getMessageType(); void setMessageType(MessageType messageType); Method getMethod(); void setMethod(Method method); synchronized void setMessageId(int messageID); synchronized int getMessageId(); byte[] getPayload(); String getPayloadString(); void setPayload(String payload); void setPayload(byte[] payload); void setToken(byte[] token); byte[] getToken(); void writeTo(OutputStream outputStream); byte[] toByteArray(); Code getCode(); void setCode(Code code); boolean getMustAcknowledge(); @Override String toString(); String toString(boolean printFullPayload); String toString(boolean printFullPayload, boolean printPayloadOnlyAsHex, boolean printAddress); String toString(boolean printFullPayload, boolean printPayloadOnlyAsHex, boolean printAddress, boolean doNotPrintPayload); @Override int hashCode(); @Override boolean equals(Object obj); static final byte[] DEFAULT_TOKEN; }### Answer:
@Test(expected = IllegalStateException.class) public void failWhenIOExceptionWhenWriting() throws Exception { OutputStream outputStream = mock(OutputStream.class); doThrow(new IOException()).when(outputStream).write(any()); new CoapPacket(null).writeTo(outputStream); }