Source:
private void prepareMocks() { BackendInternal backendMock = mock(BackendInternal.class); VDSBrokerFrontend vdsBroker = mock(VDSBrokerFrontend.class); DiskImage existingDiskImage = mock(DiskImage.class); when(existingDiskImage.getId()).thenReturn(existingDiskId); List<DiskImage> existingDiskImages = Collections.singletonList(existingDiskImage); VDSReturnValue volListReturnValue = new VDSReturnValue(); volListReturnValue.setSucceeded(true); volListReturnValue.setReturnValue(importDiskIds); doReturn(volListReturnValue).when(vdsBroker).runVdsCommand(eq(VDSCommandType.GetImagesList), any(GetImagesListVDSCommandParameters.class)); when(backendMock.runInternalQuery(eq(VdcQueryType.GetUnregisteredDisk), any(GetUnregisteredDiskQueryParameters.class), any(EngineContext.class))) .thenAnswer(invocation -> { GetUnregisteredDiskQueryParameters params1 = (GetUnregisteredDiskQueryParameters) invocation .getArguments()[1]; VdcQueryReturnValue unregDiskReturnValue = new VdcQueryReturnValue(); unregDiskReturnValue.setSucceeded(true); DiskImage newDiskImage = mock(DiskImage.class); when(newDiskImage.getId()).thenReturn(params1.getDiskId()); unregDiskReturnValue.setReturnValue(newDiskImage); return unregDiskReturnValue; }); doReturn(storagePoolId).when(getQuery()).getStoragePoolId(); doReturn(storageDomainId).when(getQuery()).getStorageDomainId(); DbFacade dbFacadeMock = getDbFacadeMockInstance(); DiskImageDao diskImageDaoMock = mock(DiskImageDao.class); StorageDomainDao storageDomainDaoMock = mock(StorageDomainDao.class); when(diskImageDaoMock.getAllSnapshotsForStorageDomain(eq(storageDomainId))).thenReturn(existingDiskImages); when(dbFacadeMock.getDiskImageDao()).thenReturn(diskImageDaoMock); StorageDomain storageDomain = new StorageDomain(); when(storageDomainDaoMock.getForStoragePool(storageDomainId, storagePoolId)).thenReturn(storageDomain); doReturn(backendMock).when(getQuery()).getBackend(); doReturn(vdsBroker).when(getQuery()).getVdsBroker(); }

Target:
private void prepareMocks() { BackendInternal backendMock = mock(BackendInternal.class); VDSBrokerFrontend vdsBroker = mock(VDSBrokerFrontend.class); DiskImage existingDiskImage = mock(DiskImage.class); when(existingDiskImage.getId()).thenReturn(existingDiskId); List<DiskImage> existingDiskImages = Collections.singletonList(existingDiskImage); VDSReturnValue volListReturnValue = new VDSReturnValue(); volListReturnValue.setSucceeded(true); volListReturnValue.setReturnValue(importDiskIds); doReturn(volListReturnValue).when(vdsBroker).runVdsCommand(eq(VDSCommandType.GetImagesList), any(GetImagesListVDSCommandParameters.class)); when(backendMock.runInternalQuery(eq(VdcQueryType.GetUnregisteredDisk), any(GetUnregisteredDiskQueryParameters.class), any(EngineContext.class))) .thenAnswer(invocation -> { GetUnregisteredDiskQueryParameters p = (GetUnregisteredDiskQueryParameters) invocation .getArguments()[1]; VdcQueryReturnValue unregDiskReturnValue = new VdcQueryReturnValue(); unregDiskReturnValue.setSucceeded(true); DiskImage newDiskImage = mock(DiskImage.class); when(newDiskImage.getId()).thenReturn(p.getDiskId()); unregDiskReturnValue.setReturnValue(newDiskImage); return unregDiskReturnValue; }); doReturn(storagePoolId).when(getQuery()).getStoragePoolId(); doReturn(storageDomainId).when(getQuery()).getStorageDomainId(); DbFacade dbFacadeMock = getDbFacadeMockInstance(); DiskImageDao diskImageDaoMock = mock(DiskImageDao.class); StorageDomainDao storageDomainDaoMock = mock(StorageDomainDao.class); when(diskImageDaoMock.getAllSnapshotsForStorageDomain(eq(storageDomainId))).thenReturn(existingDiskImages); when(dbFacadeMock.getDiskImageDao()).thenReturn(diskImageDaoMock); StorageDomain storageDomain = new StorageDomain(); when(storageDomainDaoMock.getForStoragePool(storageDomainId, storagePoolId)).thenReturn(storageDomain); doReturn(backendMock).when(getQuery()).getBackend(); doReturn(vdsBroker).when(getQuery()).getVdsBroker(); }