title: How to mock Netty?
body: <p>I just inherited a project using netty and it seems to entangle itself in the code in that many handlers are created implementing netty interfaces, etc. I want to mock it in such a way that I can add tests to test from the netty api upwards, firing bytes in and receiving bytes back but not connecting a socket to do so. Is there any way to fire into the pipeline myself which would test netty code as well as our code and also receive from the code?</p>
  
  <p>Specifically, I am looking to do something like this:</p>
  
  <pre><code>SysUnderTest(channel) {
    channel.addReadListener(myListenerImpl);
  }
  
  Test {
     testStuff() {
         channelMock = new MockChannel(); 
         SysUnderTest system = new SysUnderTest(channelMock);
         //BIG NOTE: The get here is just getting what the mocked cached when the SysUnderTest
         //ended up calling the channel.addReadLsitener(listenerImpl)!!!
         ReadListener listenerImplToTest = channelMock.getAddedListenerImpl();
         listenerImplToTest.fireNewDataIncoming(byteBuffer1);
         Assert.Equals("new state", system.getState());
         listenerImplToTest.fireNewDataIncoming(byteBuffer2);
         Assert.Equals("state2", system.getState());
     }
  }
  </code></pre>
  
  <p>Maybe I need a paradigm shift but I think any system testing with an IO library should be firing data to a listener that will react. My test needs to fire data in by invoking the listener implementation's event method (and it gets the listenerImpl though the mock channel), but I don't see how to do this in netty. Specifically, which method is firing data in for the example above. It looks like it's reading the data (which seems more like a blocking/polling thing). Maybe I need to change my paradigm of the way I am thinking about this though?</p>