assertThrows在带有Tasklet的Spring批处理中失败

p4tfgftt  于 2023-02-11  发布在  Spring
关注(0)|答案(1)|浏览(110)

我在Taskletexecute()方法中基于某个条件抛出了一个IllegalStateException异常,并且我尝试像这样测试我的批处理作业:

@Test
void testJobThrowsMyException() throws Exception {
        JobParameters emptyJobParameters = new JobParameters();

        assertThrows(IllegalStateException.class, () -> jobLauncherTestUtils.launchJob(emptyJobParameters));
}

下面是execute方法:

@Override
public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) {
        getWorkload(chunkContext).forEach(workloadItem -> {
            // Processing 
        });

        return RepeatStatus.FINISHED;
    }

这基本上就是它获得工作负载的方式:

private int getWorkload() {
        if (condition) {
            throw new IllegalStateException("Thats the exception i want to test");
        }
        
        return workload;
    }

问题是,根据测试执行的日志,虽然抛出了异常,但测试失败并显示以下消息:
org.opentest4j.AssertionFailedError: Expected java.lang.IllegalStateException to be thrown, but nothing was thrown.
但是正如前面所说的,日志清楚地表明抛出了异常:

10:32:54.608 [main] DEBUG org.hibernate.loader.Loader - Result row: 
10:32:54.616 [main] DEBUG org.springframework.batch.core.step.tasklet.TaskletStep - Applying contribution: [StepContribution: read=0, written=0, filtered=0, readSkips=0, writeSkips=0, processSkips=0, exitStatus=EXECUTING]
10:32:54.616 [main] DEBUG org.springframework.batch.core.step.tasklet.TaskletStep - Rollback for RuntimeException: java.lang.IllegalStateException: Es ist noch keine Liefernummer in den ImportDetails vorhanden.
10:32:54.618 [main] DEBUG org.springframework.transaction.support.TransactionTemplate - Initiating transaction rollback on application exception
java.lang.IllegalStateException: Thats the exception i want to test
at [...]
10:32:54.619 [main] DEBUG org.springframework.orm.jpa.JpaTransactionManager - Initiating transaction rollback
10:32:54.619 [main] DEBUG org.springframework.orm.jpa.JpaTransactionManager - Rolling back JPA transaction on EntityManager [SessionImpl(1793816221<open>)]
10:32:54.619 [main] DEBUG org.hibernate.engine.transaction.internal.TransactionImpl - rolling back
10:32:54.620 [main] DEBUG org.springframework.orm.jpa.JpaTransactionManager - Closing JPA EntityManager [SessionImpl(1793816221<open>)] after transaction
10:32:54.621 [main] DEBUG org.springframework.batch.repeat.support.RepeatTemplate - Handling exception: java.lang.IllegalStateException, caused by: java.lang.IllegalStateException: Es ist noch keine Liefernummer in den ImportDetails vorhanden.
10:32:54.621 [main] DEBUG org.springframework.batch.repeat.support.RepeatTemplate - Handling fatal exception explicitly (rethrowing first of 1): java.lang.IllegalStateException: Es ist noch keine Liefernummer in den ImportDetails vorhanden.
10:32:54.622 [main] ERROR org.springframework.batch.core.step.AbstractStep - Encountered an error executing step my-step in job my-job

我没有捕捉到这个异常,那么为什么测试失败了,有人能解释一下吗?
最好的问候

h79rfbju

h79rfbju1#

当在作业的某个步骤中抛出异常时,它不会被抛出到作业的客户端,而是保存在JobExecutionfailureExceptions中。
在您的示例中,您需要获取jobLauncherTestUtils返回的作业执行的句柄,并Assert该异常是JobExecution#failureExceptions的一部分。

@Test
void testJobThrowsMyException() throws Exception {
   // given
   JobParameters emptyJobParameters = new JobParameters();

   // when
   JobExecution jobExecution = jobLauncherTestUtils.launchJob(emptyJobParameters);

   // then
   assert that jobExecution.failureExceptions contains an IllegalStateException
}

相关问题