如何在 mockito+J-单元测试中捕获或模拟意外异常?



这是我需要编写测试的主文件中的功能。

@Override
public 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);
    mockjobParameters = new JobParametersBuilder().addLong("time", System.currentTimeMillis()).toJobParameters();
    when(mockJobLauncher.run(mockjob, mockjobParameters)).thenReturn(mockJobExecution);
    when(mockJobExecution.getStatus()).thenReturn(BatchStatus.FAILED);
    when(mockJobExecution.getJobConfigurationName()).thenReturn(null);
    Mockito.doThrow(new JobParametersInvalidException("Invalid")).when(mockJobLauncher).run(mockjob, mockjobParameters);
    inputFileLoaderTaskProcessor.processTask(mockjobExecutionContext);
}

当我将项目作为J-UNIT测试执行时,会引发其期望的处理例外,但我提到了预期的" JobParameterSinvalidexception"。

您可以看到,我只在此功能中添加了一个例外,以介绍主函数中的所有异常(内部捕获(需要做什么?

Mockito.doThrow(new JobParametersInvalidException("Invalid")).when(mockJobLauncher).run(mockjob, mockjobParameters);

由于参数不匹配,因此没有调用此Dothrow。

在代码中,参数设置为:

   JobParameters jobParameters = new JobParametersBuilder()
                                            .addLong("time", System.currentTimeMillis())
                                            .toJobParameters();

在单元测试中,匹配模拟设置为:

mockjobParameters = new JobParametersBuilder().addLong("time", System.currentTimeMillis()).toJobParameters();

麻烦是,几乎可以肯定的是,在设置测试时以及测试执行的代码时,毫无疑问是在勾选的。这意味着这些参数将不相等,因此没有抛出例外。

更改模拟设置要期望任何Jobparameters,例如:

Mockito.doThrow(new JobParametersInvalidException("Invalid")).when(mockJobLauncher).run(eq(mockjob), any(JobParameters.class));

相关内容

  • 没有找到相关文章

最新更新