@Transactional
    public void payInstallment(String identificationNumber, int creditId, int installmentId) {
        Credit credit = creditRepository.findByIdAndIdentificationNumber(creditId, identificationNumber)
                .orElseThrow(() -> createCreditNotFoundException(creditId));

        Installment installment = installmentRepository.findByCredit(credit)
                .stream()
                .filter(c -> c.getStatus() == InstallmentStatus.UNPAID)
                .filter(c -> c.getId() == installmentId)
                .findFirst()
                .orElseThrow(() -> createInstallmentNotFoundException(installmentId));

        installment.setStatus(InstallmentStatus.PAID);
        Installment savedInstallment = installmentRepository.save(installment);
        eventPublisherService.publishInstallment(getInstallmentDocumentFromInstallment(savedInstallment, credit));

        if (installmentRepository.findByCredit(credit).stream().noneMatch(c -> c.getStatus() == InstallmentStatus.UNPAID)) {
            credit.setStatus(CreditStatus.FINISHED);
            Credit finishedCredit = creditRepository.save(credit);
            eventPublisherService.publishPaidCreditInstallment(finishedCredit);
        }

        eventPublisherService.publishCredit(getCreditDocumentFromCredit(credit));
    }
@Test
    void payInstallment_installmentNotFound() {
        // Arrange
        String identificationNumber = "1234567890";
        int creditId = 1;
        int installmentId = 1;
        Credit credit = Credit.builder()
                .id(creditId)
                .amount(BigDecimal.valueOf(1000))
                .status(CreditStatus.STARTED)
                .identificationNumber(identificationNumber)
                .build();
        when(creditRepository.findByIdAndIdentificationNumber(creditId, identificationNumber)).thenReturn(Optional.of(credit));
        when(installmentRepository.findByCredit(credit)).thenReturn(List.of());

        // Act & Assert
        assertThrows(GenericException.class, () -> creditService.payInstallment(identificationNumber, creditId, installmentId));
        verify(installmentRepository, never()).save(any(Installment.class));
        verify(eventPublisherService, never()).publishInstallment(any(InstallmentDocument.class));
        verify(eventPublisherService, never()).publishCredit(any(CreditDocument.class));
    }