mercredi 31 août 2016

Mockito: Testing behavior when mock objects cant be injected

I have a method defined as

@Startup
@Singleton
public class PRCConnectionRunner { 

@Inject 
private ServiceStatus status;

private Timer connectorTimer = null;

public void destroy() {
    connectorTimer.cancel();
    status.stopped();
}
}

I want to test the behavior of destroy that it calls both stopped and cancel. I can test for stopped easily by injecting a mock object of status as below

@Mock
ServiceStatus status;

@InjectMocks
PRCConnectionRunner prcConnection;


@Test
public void destroyShouldCallStatusStop() {
    prcConnection.destroy();
    Mockito.verify(status).stopped();
}

However since i cant inject connectorTimer as it is constructed inside the PRCConnectionRunner class, how can i test that the destroy calls cancel() on connectorTimer?

Aucun commentaire:

Enregistrer un commentaire