这是我需要为其编写测试的主文件中的函数。@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()); } }}这是我试图创建的测试函数@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);}当我将项目作为 J 单元测试执行时,它期望抛出处理异常,但我在预期中提到了“JobParametersInvalidException”。如您所见,我在此函数中只添加了一个异常,为了覆盖主函数中的所有异常(在 catch 内)需要做什么?
1 回答
慕尼黑的夜晚无繁华
TA贡献1864条经验 获得超6个赞
Mockito.doThrow(new JobParametersInvalidException("Invalid")).when(mockJobLauncher).run(mockjob, mockjobParameters);
此 doThrow 未被调用,因为参数不匹配。
在代码中,参数设置为:
JobParameters jobParameters = new JobParametersBuilder() .addLong("time", System.currentTimeMillis()) .toJobParameters();
在单元测试中,匹配的 mock 设置为:
mockjobParameters = new JobParametersBuilder().addLong("time", System.currentTimeMillis()).toJobParameters();
问题是,毫秒时钟几乎肯定会在设置测试和执行被测代码之间滴答作响。这意味着这些参数将不相等,因此不会抛出异常。
更改模拟设置以期望任何 JobParameters,例如:
Mockito.doThrow(new JobParametersInvalidException("Invalid")).when(mockJobLauncher).run(eq(mockjob), any(JobParameters.class));
添加回答
举报
0/150
提交
取消