### Question:
OFEncoder extends MessageToByteEncoder<MessageListenerWrapper> { @Override protected void encode(final ChannelHandlerContext ctx, final MessageListenerWrapper wrapper, final ByteBuf out) throws Exception { LOG.trace("Encoding"); try { serializationFactory.messageToBuffer(wrapper.getMsg().getVersion(), out, wrapper.getMsg()); if(wrapper.getMsg() instanceof FlowModInput){ statisticsCounters.incrementCounter(CounterEventTypes.DS_FLOW_MODS_SENT); } statisticsCounters.incrementCounter(CounterEventTypes.DS_ENCODE_SUCCESS); } catch(final Exception e) { LOG.warn("Message serialization failed ", e); statisticsCounters.incrementCounter(CounterEventTypes.DS_ENCODE_FAIL); if (wrapper.getListener() != null) { final Future<Void> newFailedFuture = ctx.newFailedFuture(e); wrapper.getListener().operationComplete(newFailedFuture); } out.clear(); return; } } OFEncoder(); void setSerializationFactory(final SerializationFactory serializationFactory);  }### Answer:
@Test public void testEncodeSuccess() { when(mockOut.readableBytes()).thenReturn(1); when(wrapper.getMsg()).thenReturn(mockMsg); when(wrapper.getMsg().getVersion()).thenReturn((short) EncodeConstants.OF13_VERSION_ID); try { ofEncoder.encode(mockChHndlrCtx, wrapper, mockOut); } catch (Exception e) { Assert.fail(); } verify(mockOut, times(0)).clear(); }
@Test public void testEncodeSerializationException() { when(wrapper.getMsg()).thenReturn(mockMsg); when(wrapper.getListener()).thenReturn(listener); when(wrapper.getMsg().getVersion()).thenReturn((short) EncodeConstants.OF13_VERSION_ID); doThrow(new IllegalArgumentException()).when(mockSerializationFactory).messageToBuffer(anyShort(),any(ByteBuf.class), any(DataObject.class)); try { ofEncoder.encode(mockChHndlrCtx, wrapper, mockOut); } catch (Exception e) { Assert.fail(); } verify(mockOut, times(1)).clear(); }
@Test public void testEncodeSerializesNoBytes() { when(mockOut.readableBytes()).thenReturn(0); when(wrapper.getMsg()).thenReturn(mockMsg); when(wrapper.getMsg().getVersion()).thenReturn((short) EncodeConstants.OF13_VERSION_ID); try { ofEncoder.encode(mockChHndlrCtx, wrapper, mockOut); } catch (Exception e) { Assert.fail(); } verify(mockOut, times(0)).clear(); verify(mockChHndlrCtx, times(0)).writeAndFlush(mockOut); verify(mockOut, times(0)).retain(); }