I am trying to test this code with Mockito:
@Override
public Response killServer() {
new Thread(new Runnable() {
@Override
public void run() {
Slave slave = SlaveRunner.getSlave();
slave.initiateShutdown();
}
}).start();
String host = System.getenv("HOSTNAME");
return Response.ok().entity(new String ("Stopping REST service on " + host)).build();
}
I can't run this in a unit test, as the system will exit. The initiateShutdown()
method does a System.exit();
. I have mocked this class with:
@Test
public void killServer() throws RestInterfaceException {
SlaveRestInterface mockedAgent = Mockito.mock(SlaveRemoteProxy.class);
Response r = Mockito.mock(Response.class);
Mockito.when(mockedAgent.killServer()).thenReturn(r);
Assert.assertEquals(r.readEntity(String.class), "Stopping REST service on " + System.getenv("HOSTNAME"));
r.close();
}
I can't do the assertion like this, as r will always be null. Is there any way to assert the response or do I just have to leave out assertions for this test?