我正在尝试在 gitlab 中运行单元测试,测试在我的本地环境中正常运行,但我在 Gitlab 中遇到以下错误。
我猜测 gitlab 无法访问数据库,这就是脚本无法将文件保存到数据库的原因。
这是我在 Gitlab 中收到的错误消息
Error message:
[ERROR] Failures:
[ERROR] AnnualReturnServiceTest.testSave:102
Wanted but not invoked:
annualReturnRepository.save(
<any com.riskanalysisexpertsystem.scanpro.model.company.AnnualReturn>
);
-> at com.test.unit.AnnualReturnServiceTest.testSave(AnnualReturnServiceTest.java:102)
Actually, there were zero interactions with this mock.
这是 AnnualReturnServiceTest.testSave 的脚本
public void testSave() {
logger.info("Mock file and file details");
pdftoImageConverterUtility.convertFile(SOURCE_FILE_PATH, SOURCE_DESTINATION_PATH);
File file = new File(SOURCE_DESTINATION_PATH);
String fileDetails = "Annual Return Form\n"
+ "Company Name: Apple Inc.\n"
+ "\n"
+ "TIN: 3243243\n"
+ "\n"
+ "Company Address: 32432 MaryDale Drive., Ghana 234324\n"
+ "Company Registration Number: 432432\n"
+ "Fiscal Year Start Date: 2022-01\n"
+ "\n"
+ "Fiscal Year End Date: 2022-12\n"
+ "\n"
+ "Total Revenue: 342432\n"
+ "\n"
+ "Total Expenses: 3434\n"
+ "\n"
+ "Taxable Income: 34333\n"
+ "\n"
+ "Tax Paid: 3243";
logger.info("Mock the behaviour of OCR utility");
when(ocrUtility.performOCR(file)).thenReturn(fileDetails);
logger.info("Call the save method");
annualReturnService.save(file);
logger.info("Verify that parseAnnualReturnDetails is not null");
try {
assertNotNull(annualReturnService.parseAnnualReturnDetails(fileDetails));
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
logger.info("Verify the annualReturnRepository's save method is called");
verify(annualReturnRepository, times(1)).save(any(AnnualReturn.class));
}
.gitlab-ci
test:
stage: test
script:
- mvn test -Dtest=com.test.unit.AnnualReturnServiceTest
allow_failure: true
dependencies:
- build
artifacts:
paths:
- target/surefire-reports
expire_in: "30 days"
来自 gitlab 的错误看起来像是 Mockito 验证错误,与数据库访问无关。
造成这种情况有两个明显的原因。
也许您可以提供测试定义和被测服务中的一些代码。
PS,如果您只想验证单个方法调用,可以跳过
times(1)
verify(annualReturnRepository).save(any(AnnualReturn.class));