### Question:
TelnetProcessHandler extends SimpleChannelInboundHandler<String> { @Override protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception { if (StringUtils.isBlank(msg)) { ctx.writeAndFlush(QosProcessHandler.prompt); } else { CommandContext commandContext = TelnetCommandDecoder.decode(msg); commandContext.setRemote(ctx.channel()); try { String result = commandExecutor.execute(commandContext); if (StringUtils.isEquals(QosConstants.CLOSE, result)) { ctx.writeAndFlush(getByeLabel()).addListener(ChannelFutureListener.CLOSE); } else { ctx.writeAndFlush(result + QosConstants.BR_STR + QosProcessHandler.prompt); } } catch (NoSuchCommandException ex) { ctx.writeAndFlush(msg + " :no such command"); ctx.writeAndFlush(QosConstants.BR_STR + QosProcessHandler.prompt); log.error("can not found command " + commandContext, ex); } catch (Exception ex) { ctx.writeAndFlush(msg + " :fail to execute commandContext by " + ex.getMessage()); ctx.writeAndFlush(QosConstants.BR_STR + QosProcessHandler.prompt); log.error("execute commandContext got exception " + commandContext, ex); } } }    }### Answer:
@Test public void testPrompt() throws Exception { ChannelHandlerContext context = mock(ChannelHandlerContext.class); TelnetProcessHandler handler = new TelnetProcessHandler(); handler.channelRead0(context, ""); verify(context).writeAndFlush(QosProcessHandler.prompt); }
@Test public void testBye() throws Exception { ChannelHandlerContext context = mock(ChannelHandlerContext.class); TelnetProcessHandler handler = new TelnetProcessHandler(); ChannelFuture future = mock(ChannelFuture.class); when(context.writeAndFlush("BYE!\n")).thenReturn(future); handler.channelRead0(context, "quit"); verify(future).addListener(ChannelFutureListener.CLOSE); }
@Test public void testUnknownCommand() throws Exception { ChannelHandlerContext context = mock(ChannelHandlerContext.class); TelnetProcessHandler handler = new TelnetProcessHandler(); handler.channelRead0(context, "unknown"); ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); verify(context, Mockito.atLeastOnce()).writeAndFlush(captor.capture()); assertThat(captor.getAllValues(), contains("unknown :no such command", "\r\ndubbo>")); }
@Test public void testGreeting() throws Exception { ChannelHandlerContext context = mock(ChannelHandlerContext.class); TelnetProcessHandler handler = new TelnetProcessHandler(); handler.channelRead0(context, "greeting"); ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); verify(context).writeAndFlush(captor.capture()); assertThat(captor.getValue(), containsString("greeting")); assertThat(captor.getValue(), containsString("dubbo>")); }