gpt4 book ai didi

java - 当方法的返回值是模拟期望时如何进行单元测试

转载 作者:行者123 更新时间:2023-12-01 22:31:44 25 4
gpt4 key购买 nike

方法“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());
}

现在,contracts参数的值为空。应该是返回contractList。我认为是因为 checkPgwContract 方法的模拟被忽略了。

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

最佳答案

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

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

关于java - 当方法的返回值是模拟期望时如何进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58546479/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com