我试图在gitlab中运行一个单元测试,测试在我的本地环境中运行正常,但是我在Gitlab中遇到了下面的错误.
我猜gitlab没有访问数据库的权限,这就是脚本无法将文件保存到数据库的原因.
以下是我在Gitlab中收到的错误信息
Error message:
[ERROR] Failures:
[ERROR] 年度退货服务Test.testSave:102
Wanted but not invoked:
annualReturnRepository.save(
<any com.riskanalysisexpertsystem.scanpro.model.company.AnnualReturn>
);
-> at com.test.unit.年度退货服务Test.testSave(年度退货服务Test.java:102)
Actually, there were zero interactions with this mock.
下面是年度退货服务Test.testSave的脚本
public class 年度退货服务Test{
private Logger logger;
private PDFtoImageConverterUtility pdftoImageConverterUtility;
private final String SOURCE_FILE_PATH = "../scanpro/src/test/resources/files/taxdocuments/company/annual_return_form.pdf";
private final String SOURCE_DESTINATION_PATH = "../scanpro/src/test/resources/files/taxdocuments/company/annual_return.png";
@Mock
private AnnualReturnRepository annualReturnRepository;
@Mock
private OcrUtility ocrUtility;
@InjectMocks
private 年度退货服务 annualReturnService;
@BeforeClass
public void setUpBeforeClass() throws Exception{
logger = LogManager.getLogger(年度退货服务Test.class);
pdftoImageConverterUtility = new PDFtoImageConverterUtility();
MockitoAnnotations.openMocks(this);
}
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));
}
}
年度退货服务
@Service
@RequiredArgsConstructor
public class 年度退货服务 {
private final AnnualReturnRepository annualReturnRepository;
public void save(File file) {
OcrUtility ocrUtility = new OcrUtility();
String fileDetails = ocrUtility.performOCR(file);
if (fileDetails != null) {
AnnualReturn annualReturn = new AnnualReturn();
try {
annualReturn = parseAnnualReturnDetails(fileDetails);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (annualReturn != null) {
annualReturnRepository.save(annualReturn);
}
}
}
快走,快走!
test:
stage: test
script:
- mvn test -Dtest=com.test.unit.年度退货服务Test
allow_failure: true
dependencies:
- build
artifacts:
paths:
- target/surefire-reports
expire_in: "30 days"