### Question:
CmsAspspPiisServiceInternal implements CmsAspspPiisService { @Override @Transactional public boolean terminateConsent(@NotNull String consentId, @NotNull String instanceId) { Optional<ConsentEntity> entityOptional = consentJpaRepository.findOne(piisConsentEntitySpecification.byConsentIdAndInstanceId(consentId, instanceId)); if (entityOptional.isEmpty()) { log.info("Consent ID: [{}], Instance ID: [{}]. PIIS consent cannot be terminated, because it was not found by consentId and instanceId", consentId, instanceId); return false; } ConsentEntity entity = entityOptional.get(); changeStatusAndLastActionDate(entity, TERMINATED_BY_ASPSP); piisConsentLazyMigrationService.migrateIfNeeded(entity); return true; }  @Override @Transactional Optional<String> createConsent(@NotNull PsuIdData psuIdData, @NotNull CreatePiisConsentRequest request, @NotNull String instanceId); @Override @Transactional @NotNull List<CmsPiisConsent> getConsentsForPsu(@NotNull PsuIdData psuIdData, @NotNull String instanceId); @Override @Transactional boolean terminateConsent(@NotNull String consentId, @NotNull String instanceId);  }### Answer:
@Test void terminateConsent_Success() { when(piisConsentEntitySpecification.byConsentIdAndInstanceId(CONSENT_EXTERNAL_ID, DEFAULT_SERVICE_INSTANCE_ID)) .thenReturn((root, criteriaQuery, criteriaBuilder) -> null); when(consentJpaRepository.findOne(any(Specification.class))).thenReturn(Optional.of(buildPiisConsentEntity())); ArgumentCaptor<ConsentEntity> argumentCaptor = ArgumentCaptor.forClass(ConsentEntity.class); ConsentEntity modified = buildPiisConsentEntity(); modified.setLastActionDate(LocalDate.now()); modified.setConsentStatus(ConsentStatus.TERMINATED_BY_ASPSP); when(piisConsentLazyMigrationService.migrateIfNeeded(any(ConsentEntity.class))).thenReturn(modified); boolean actual = cmsAspspPiisServiceInternal.terminateConsent(CONSENT_EXTERNAL_ID, DEFAULT_SERVICE_INSTANCE_ID); assertTrue(actual); verify(piisConsentLazyMigrationService).migrateIfNeeded(argumentCaptor.capture()); assertEquals(ConsentStatus.TERMINATED_BY_ASPSP, argumentCaptor.getValue().getConsentStatus()); verify(piisConsentEntitySpecification, times(1)) .byConsentIdAndInstanceId(CONSENT_EXTERNAL_ID, DEFAULT_SERVICE_INSTANCE_ID); }
@Test void terminateConsent_Failure_WrongConsentId() { boolean actual = cmsAspspPiisServiceInternal.terminateConsent(CONSENT_EXTERNAL_ID_WRONG, DEFAULT_SERVICE_INSTANCE_ID); assertFalse(actual); verify(consentJpaRepository, never()).save(any(ConsentEntity.class)); verify(piisConsentEntitySpecification, times(1)) .byConsentIdAndInstanceId(CONSENT_EXTERNAL_ID_WRONG, DEFAULT_SERVICE_INSTANCE_ID); }