### Question:
AbstractSessionHandler extends MessageToMessageCodec<FixMessage, FixMessageBuilder> { @Override public void channelInactive(ChannelHandlerContext ctx) { Attribute<FixSession> fixSessionAttribute = ctx.channel().attr(FIX_SESSION_KEY); if (fixSessionAttribute != null) { FixSession session = fixSessionAttribute.getAndSet(null); if (session != null) { ctx.fireChannelRead(new LogoutEvent(session)); sessionRepository.removeSession(session.getId()); getLogger().info("Fix Session Closed. {}", session); } } } protected  AbstractSessionHandler(FixApplication fixApplication,
                                     FixClock fixClock,
                                     SessionRepository sessionRepository); @Override void channelInactive(ChannelHandlerContext ctx); @Override void exceptionCaught(ChannelHandlerContext ctx, Throwable cause); static final AttributeKey<FixSession> FIX_SESSION_KEY; }### Answer:
@Test public void testChannelInactiveSessionExists() throws Exception { when(channel.attr(AbstractSessionHandler.FIX_SESSION_KEY)).thenReturn(sessionAttr); when(sessionAttr.getAndSet(null)).thenReturn(fixSession); SessionId sessionId = mock(SessionId.class); when(fixSession.getId()).thenReturn(sessionId); sessionHandler.channelInactive(ctx); verify(ctx).fireChannelRead(any(LogoutEvent.class)); verify(sessionAttr).getAndSet(null); verify(sessionRepository).removeSession(sessionId); }
@Test public void testChannelInactiveNoKeyAttr() throws Exception { when(channel.attr(AbstractSessionHandler.FIX_SESSION_KEY)).thenReturn(null); sessionHandler.channelInactive(ctx); verify(ctx, never()).fireChannelRead(any(LogoutEvent.class)); }
@Test public void testChannelInactiveSessionNotExists() throws Exception { when(channel.attr(AbstractSessionHandler.FIX_SESSION_KEY)).thenReturn(sessionAttr); when(sessionAttr.getAndSet(null)).thenReturn(null); sessionHandler.channelInactive(ctx); verify(ctx, never()).fireChannelRead(any(LogoutEvent.class)); }