Q:

Call returning Future throws NPE in unit test

I'm trying to unit test some of my client code that looks like the following:
@Override
public void stop() {

    if (client != null) {
        Future<Void> disconnectClient = client.disconnect();

        try {
            disconnectClient.await();
        } catch (InterruptedException e) {
            logger.info("Failed to disconnect client: ", e.getLocalizedMessage());
        }
    } else {
        logger.error("Client does not exist.");
    }
}

where the disconnect() returns a Future<Void>.  A simple test looks like:
@Mock
private MyClient client;

@InjectMocks
private MyServer objectUnderTest = new MyServer();

@Test
public void shouldCallDisconnectOnClientWhenDefined() throws Exception {
    objectUnderTest.stop();
    verify(client, times(1)).disconnect();
}

When I run this, I obviously get a NPE on the Future object.  I cant think of any when() code that can successfully return me a mocked Future or anything like that.  How could I get this?
MyServer contains a client connector, that connects to an outside server upon initialisation of MyServer.  Multiple MyServers can be created, each with their own channel to the outside server, through their client.  All of the instances of MyServer are maintained by a MyServerRegistry.  When a connection is no longer required, the client will disconnect, and the MyServer instance removed from the registry.
Note! client is not Autowired into MyServer, but instead initalised and a connection opened in the constructor of MyServer.  I also have no control over the (non-final) disconnect() call returning a Future<Void>.

A:

What about the simple:
when(client.disconnect()).thenReturn(mockedFuture);

Or am I overlooking something here? You can verify the method call, so why do you think you can't mock it?!
The client object is your object. You already mocked it. So what makes you think you don't have control over its disconnect() method? That method is called on your mock; it returns a value, so you can mock it!
( unless MyClient or disconnect() is final. then you need to use newer versions of Mockito 2 supporting "final-mocking" as experimental feature )

