ClientChannelFactory implements AutoCloseable { public ListenableFuture<RpcChannel> createChannel(String server, int port) { Bootstrap b = new Bootstrap(); b.group(group) .channel(NioSocketChannel.class) .handler(init); final ChannelFuture channel = b.connect(server, port); final ExposedListenableFuture<RpcChannel> onDone = new ExposedListenableFuture<>(); channel.addListener(new GenericFutureListener<Future<Void>>() { @Override public void operationComplete(Future<Void> future) throws Exception { if (!future.isSuccess()) { Throwable error = future.cause(); onDone.setException(error); return; } Channel ch = channel.channel(); channels.add(ch); onDone.set(new RpcChannel(ch, services, requestManager)); } }); return onDone; } ClientChannelFactory(List<ProtobufServiceTranslator> services,
      ClientChannelInitializer initializer, RequestManager requestManager); @Override void close(); ListenableFuture<RpcChannel> createChannel(String server, int port);  }
@Test public void testConnectNoServer() throws Exception { RequestManager requestManager = Mockito.mock(RequestManager.class); MessageMapper mapper = Mockito.mock(MessageMapper.class); ClientChannelInitializer channelInitializer = new ClientChannelInitializer(mapper, requestManager); ClientChannelFactory factory = new ClientChannelFactory(new ArrayList<ProtobufServiceTranslator>(), channelInitializer, requestManager); ListenableFuture<RpcChannel> channel = factory.createChannel("localhost", 12345); try { Futures.get(channel, ConnectException.class); fail("Should not be successful"); } catch (ConnectException e) { } Mockito.verifyZeroInteractions(mapper, requestManager); }