### Question:
CoapTcpPacketSerializer { public static void writeTo(OutputStream os, CoapPacket coapPacket) throws CoapException, IOException { ByteArrayOutputStream headerOptionsStream = new ByteArrayOutputStream(); coapPacket.headers().serialize(headerOptionsStream); int tokenLen = coapPacket.getToken().length; if (tokenLen > 8) { throw new CoapException("Token length should not exceed 8 bytes"); } int optionsLength = headerOptionsStream.size(); int payloadLen = coapPacket.getPayload().length; int payloadMarkerLen = payloadLen > 0 ? 1 : 0; int packetLength = optionsLength + payloadMarkerLen + payloadLen; int packetLen1Code = packetLenCode(packetLength); write8(os, (packetLen1Code << 4) | tokenLen); writeExtendedPacketLength(os, packetLen1Code, packetLength); CoapPacket.writeCode(os, coapPacket); os.write(coapPacket.getToken()); os.write(headerOptionsStream.toByteArray()); if (coapPacket.getPayload() != null && coapPacket.getPayload().length > 0) { os.write(CoapPacket.PAYLOAD_MARKER); os.write(coapPacket.getPayload()); } }  static CoapPacket deserialize(InetSocketAddress remoteAddress, InputStream inputStream); static Optional<CoapPacket> deserializeIfEnoughData(InetSocketAddress remoteAddress, InputStream inputStream); static byte[] serialize(CoapPacket coapPacket); static void writeTo(OutputStream os, CoapPacket coapPacket);  }### Answer:
@Test(expected = IOException.class) public void outputStremException() throws CoapException, IOException { OutputStream os = Mockito.mock(OutputStream.class); doThrow(new IOException()).when(os).write(any()); CoapPacket cp = new CoapPacket(null, null, "", null); CoapTcpPacketSerializer.writeTo(os, cp); }