方法“ ContractService”的返回是方法“ checkPgwContract”的返回值。我已经在模拟ContractService类和ContractServiceManager类,但是checkPgwContract方法始终在我的单元测试中通过。

ContractServiceImpl

@Override
public List getListOfContractBy(String contractNo) {
List<OasysContract> oasysContractList = oasysContractRepository.findByContractNumber(contractNo);
if (!oasysContractList.isEmpty()) {
        return oasysContractList;
    } else {
        List<Contract> pgwContractList = contractRepository.findContractsByContractNumber(contractNo);
        if (!pgwContractList.isEmpty()) {
            return contractServiceManager.checkPgwContract(pgwContractList);
        }
    }
    return new ArrayList();
}


ContractServiceManagerImpl

private boolean checkHCP(String contractNumber) {
    return contractRepository.findContractByExpiredDate(contractNumber) != null ? true : false;
}

private String checkIsFullyPaid(String contractNumber) {
    return contractRepository.getFullyPaid(contractNumber);
}

@Override
public List<Contract> checkPgwContract(List<Contract> contractList) {
    for (Contract contract : contractList) {
        //check paymentType
        if (contract.getPaymentType() != null &&
                contract.getPaymentType().equals(PAYMENT_TYPE_HCP)) {
            if (checkHCP(contract.getContractNumber())) {
                //check isFullyPaid
                if (checkIsFullyPaid(contract.getContractNumber()).equals(FLAG_YES)) {
                    return new ArrayList<>();
                }
            } else {
                return new ArrayList<>();
            }
        }
    }
    return contractList;
}


单元测试

@InjectMocks
@Spy
private ContractServiceImpl service;

@Mock
ContractRepository contractRepository;

@Mock
OasysContractRepository oasysContractRepository;

@Mock
ContractServiceManager serviceManager;


public static final String contractNumber = "3900006835";

@Test
public void getListOfContractBy_hcp_success() {
    List<OasysContract> oasysContractList = new ArrayList<>();
    oasysContractList.isEmpty();

    List<Contract> contractList = new ArrayList<>();
    contractList.add(BuildUtil.buildContract());

    //mock
    Mockito.doReturn(oasysContractList).when(oasysContractRepository).findByContractNumber(contractNumber);
    Mockito.doReturn(BuildUtil.buildContractList()).when(contractRepository).findContractsByContractNumber(contractNumber);
    Mockito.doReturn(contractList).when(serviceManager).checkPgwContract(contractList);
    Mockito.doReturn(BuildUtil.buildContract()).when(contractRepository).findContractByExpiredDate(contractNumber);
    Mockito.doReturn("N").when(contractRepository).getFullyPaid(contractNumber);


    //test
    List<Contract> contracts = service.getListOfContractBy(contractNumber);

    System.out.println(contracts);

    assert(!contracts.isEmpty());
}


现在,合同价值参数为空。它应该是return contractList。我认为是因为方法checkPgwContract的模拟被忽略了。

我的测试仍然会通过,这显然是错误的,那么我应该如何测试呢?

最佳答案

这行对我来说没有意义:Mockito.doReturn(contractList).when(serviceManager).checkPgwContract(contractList);

您的方法checkPgwContract()不会以contractList作为参数调用,因此不会触发Mockito的doReturn。
上面的行中返回的BuildUtil.buildContractList()都会调用它:
Mockito.doReturn(BuildUtil.buildContractList()).when(contractRepository).findContractsByContractNumber(contractNumber);

10-04 17:46