### Question:
MySQLNegotiateHandler extends ChannelInboundHandlerAdapter { @Override public void channelRead(final ChannelHandlerContext ctx, final Object msg) { if (msg instanceof HandshakeInitializationPacket) { HandshakeInitializationPacket handshake = (HandshakeInitializationPacket) msg; ClientAuthenticationPacket clientAuth = new ClientAuthenticationPacket(); clientAuth.setSequenceNumber((byte) (handshake.getSequenceNumber() + 1)); clientAuth.setCharsetNumber((byte) 33); clientAuth.setUsername(username); clientAuth.setPassword(password); clientAuth.setServerCapabilities(handshake.getServerCapabilities()); clientAuth.setDatabaseName("mysql"); clientAuth.setAuthPluginData(joinAndCreateAuthPluginData(handshake)); clientAuth.setAuthPluginName(handshake.getAuthPluginName()); ctx.channel().writeAndFlush(clientAuth); serverInfo = new ServerInfo(); serverInfo.setServerVersion(new ServerVersion(handshake.getServerVersion())); return; } if (msg instanceof OkPacket) { ctx.channel().pipeline().remove(this); authResultCallback.setSuccess(serverInfo); return; } ErrorPacket error = (ErrorPacket) msg; ctx.channel().close(); throw new RuntimeException(error.getMessage()); }  @Override void channelRead(final ChannelHandlerContext ctx, final Object msg);  }### Answer:
@Test public void assertChannelReadHandshakeInitPacket() throws NoSuchFieldException, IllegalAccessException { HandshakeInitializationPacket handshakeInitializationPacket = new HandshakeInitializationPacket(); handshakeInitializationPacket.setServerVersion(SERVER_VERSION); handshakeInitializationPacket.setAuthPluginName(""); handshakeInitializationPacket.setServerCapabilities(1); handshakeInitializationPacket.setAuthPluginDataPart1(new byte[8]); handshakeInitializationPacket.setAuthPluginDataPart2(new byte[12]); mySQLNegotiateHandler.channelRead(channelHandlerContext, handshakeInitializationPacket); verify(channel).writeAndFlush(ArgumentMatchers.any(ClientAuthenticationPacket.class)); ServerInfo serverInfo = ReflectionUtil.getFieldValueFromClass(mySQLNegotiateHandler, "serverInfo", ServerInfo.class); assertThat(serverInfo.getServerVersion().getMajor(), is(5)); assertThat(serverInfo.getServerVersion().getMinor(), is(7)); assertThat(serverInfo.getServerVersion().getSeries(), is(13)); }
@Test public void assertChannelReadOkPacket() throws NoSuchFieldException, IllegalAccessException { OkPacket okPacket = new OkPacket(); ServerInfo serverInfo = new ServerInfo(); ReflectionUtil.setFieldValueToClass(mySQLNegotiateHandler, "serverInfo", serverInfo); mySQLNegotiateHandler.channelRead(channelHandlerContext, okPacket); verify(pipeline).remove(mySQLNegotiateHandler); verify(authResultCallback).setSuccess(serverInfo); }
@Test(expected = RuntimeException.class) public void assertChannelReadErrorPacket() { ErrorPacket errorPacket = new ErrorPacket(); mySQLNegotiateHandler.channelRead(channelHandlerContext, errorPacket); }