這是我需要為其編寫測試的主文件中的函數(shù)。@Overridepublic void processTask(JobExecutionContext arg0) throws TaskException { if (BatchInputChannel.DB.toString().equals(runtimeContext.getProperties().getProperty(BATCH_CHANNEL_TYPE))) { return; } else if (BatchInputChannel.FILE.toString().equals(runtimeContext.getProperties().getProperty(BATCH_CHANNEL_TYPE))) { jobLauncher = (JobLauncher) beanFactory.getBean("jobLauncher"); Job job = (Job) beanFactory.getBean("micorpFileLoadJob"); JobParameters jobParameters = new JobParametersBuilder() .addLong("time", System.currentTimeMillis()) .toJobParameters(); try { JobExecution jobExecution = jobLauncher.run(job, jobParameters); System.out.println("jobExecution=="+jobExecution); } catch (JobExecutionAlreadyRunningException | JobRestartException | JobInstanceAlreadyCompleteException | JobParametersInvalidException e) { throw new ProcessingException("File Loading Failed" + e.getMessage()); } }}這是我試圖創(chuàng)建的測試函數(shù)@Test(expected = JobParametersInvalidException.class)public void processTaskWithFileInputJobFailed5() throws Exception { when(mockruntimeContext.getProperties()).thenReturn(mockProperties); when(mockProperties.getProperty(BATCH_CHANNEL_TYPE)).thenReturn("FILE"); when(mockbeanFactory.getBean("jobLauncher")).thenReturn(mockJobLauncher); when(mockbeanFactory.getBean("micorpFileLoadJob")).thenReturn(mockjob);}當(dāng)我將項(xiàng)目作為 J 單元測試執(zhí)行時(shí),它期望拋出處理異常,但我在預(yù)期中提到了“JobParametersInvalidException”。如您所見,我在此函數(shù)中只添加了一個(gè)異常,為了覆蓋主函數(shù)中的所有異常(在 catch 內(nèi))需要做什么?
如何在 mockito+J-unit 測試中捕獲或模擬意外異常?
呼啦一陣風(fēng)
2023-02-23 10:01:32