@Test
public void encode() {

    // prepare
    IoSession session = mock(IoSession.class);
    ByteBuffer buff = ByteBuffer.wrap("test".getBytes());
    Object encodingState = new Object();
    when(session.getAttribute(new AttributeKey<Object>(Object.class, "internal_encoder")))
            .thenReturn(encodingState);
    Object toEncode = new Object();
    WriteRequest wrq = mock(WriteRequest.class);
    when(wrq.getMessage()).thenReturn(toEncode);
    when(encoder.encode(toEncode, encodingState)).thenReturn(buff);
    WriteFilterChainController ctrl = mock(WriteFilterChainController.class);
    // run
    filter.messageWriting(session, wrq, ctrl);
    // verify
    verify(encoder).encode(toEncode, encodingState);
    verify(wrq).getMessage();
    verify(wrq).setMessage(buff);
    verify(ctrl).callWriteNextFilter(wrq);
    verify(session).getAttribute(new AttributeKey<Object>(Object.class, "internal_encoder"));
    verifyNoMoreInteractions(encoder, decoder, session, ctrl, wrq);
}