create unit test using spring boot for this code package…
- User
create unit test using spring boot for this code package com.senpare.paymentservice.service;
import com.senpare.paymentservice.dto.LicenseRequest; import com.senpare.paymentservice.dto.PaymentLogRequest; import com.senpare.paymentservice.dto.PaymentStatus; import com.senpare.paymentservice.exception.BadRequestException; import com.senpare.paymentservice.exception.ResourceNotFoundException; import com.senpare.paymentservice.model.License; import com.senpare.paymentservice.model.PaymentLog; import com.senpare.paymentservice.repository.PaymentLogRepository; import com.senpare.paymentservice.utilities.PaymentServiceMessages; import com.senpare.paymentservice.utilities.Util; import jakarta.transaction.Transactional; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.data.domain.Page; import org.springframework.stereotype.Service;
import java.util.UUID;
@Service @RequiredArgsConstructor @Slf4j public class PaymentLogService {
private final PaymentLogRepository paymentLogRepository; private final LicenseService licenseService; public PaymentLog create(PaymentLogRequest request) { if(request == null) { throw new BadRequestException(PaymentServiceMessages.canNotBeNull("PaymentLogRequest")); } PaymentLog paymentLog = createPaymentLogFromRequest(request); return paymentLogRepository.save(paymentLog); } public PaymentLog get(UUID uuid) { if(uuid == null) { throw new BadRequestException(PaymentServiceMessages.canNotBeNull("PaymentLog.uuid")); } return paymentLogRepository.findById(uuid) .orElseThrow(() -> new ResourceNotFoundException(PaymentServiceMessages.canNotBeFound("PaymentLog", "uuid", uuid.toString()))); } public PaymentLog getByTransactionRef(String txRef) { if(Util.isNullOrEmpty(txRef)) { throw new BadRequestException(PaymentServiceMessages.canNotBeNull("PaymentLog.txRef")); } return paymentLogRepository.findByTransactionReference(txRef) .orElseThrow(() -> new ResourceNotFoundException(PaymentServiceMessages.canNotBeFound("PaymentLog","txRef", txRef))); } public Page<PaymentLog> getAll(int page, int size, String sortBy) { return Util.paginate(paymentLogRepository, page, size, sortBy); } @Transactional public PaymentLog verifyPayment(String txRef) { if(Util.isNullOrEmpty(txRef)) { throw new BadRequestException(PaymentServiceMessages.canNotBeNull("PaymentLog.txRef")); } PaymentLog paymentLog = paymentLogRepository.findByTransactionReference(txRef) .orElseThrow(() -> new ResourceNotFoundException(PaymentServiceMessages.canNotBeFound("PaymentLog","txRef", txRef))); paymentLog.setStatus(PaymentStatus.SUCCESSFUL); log.info("Creating a licence after payment verification for transaction reference: {}", txRef); LicenseRequest request = new LicenseRequest() .setEmail(paymentLog.getEmail()) .setAmount(paymentLog.getAmount()); License license = licenseService.create(request); paymentLog.setLicense(license); return paymentLogRepository.save(paymentLog); } public PaymentLog cancelPayment(String txRef) { if(Util.isNullOrEmpty(txRef)) { throw new BadRequestException(PaymentServiceMessages.canNotBeNull("PaymentLog.txRef")); } PaymentLog paymentLog = paymentLogRepository.findByTransactionReference(txRef) .orElseThrow(() -> new ResourceNotFoundException(PaymentServiceMessages.canNotBeFound("PaymentLog","txRef", txRef))); paymentLog.setStatus(PaymentStatus.FAILED); return paymentLogRepository.save(paymentLog); } public boolean isPaymentVerified(String txRef) { if(Util.isNullOrEmpty(txRef)) { throw new BadRequestException(PaymentServiceMessages.canNotBeNull("PaymentLog.txRef")); } PaymentLog paymentLog = paymentLogRepository.findByTransactionReference(txRef) .orElseThrow(() -> new ResourceNotFoundException(PaymentServiceMessages.canNotBeFound("PaymentLog","txRef", txRef))); return paymentLog.getStatus() == PaymentStatus.SUCCESSFUL; } private PaymentLog createPaymentLogFromRequest(PaymentLogRequest request) { return new PaymentLog() .setUuid(UUID.randomUUID()) .setEmail(request.getEmail()) .setPaymentProvider(request.getPaymentProvider()) .setTransactionReference(request.getTransactionReference()) .setAmount(request.getAmount()) .setStatus(PaymentStatus.INITIALIZED) ; } - Assistant
- User
in the above code how to import "expected"
- Assistant
The
@Test(expected = SomeException.class)annotation is used in JUnit 4 to specify that a test method is expected to throw a specific exception.To use this annotation, you need to add the following import statement at the beginning of your test class:
import org.junit.Test;Make sure you have the JUnit 4 dependency (junit:junit:4.x) in your project's build path, as this annotation is part of the JUnit 4 framework.