From 6b79e2c1f756bf87416baaf424813c84db8755b7 Mon Sep 17 00:00:00 2001 From: Mahmoud Ben Hassine Date: Thu, 18 Apr 2024 16:23:06 +0200 Subject: [PATCH] Refine contribution #4522 * Update tests --- .../batch/core/step/AbstractStepTests.java | 77 ++++++++++++------- 1 file changed, 48 insertions(+), 29 deletions(-) diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/step/AbstractStepTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/step/AbstractStepTests.java index 56e11f5b7..8d761fa19 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/step/AbstractStepTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/step/AbstractStepTests.java @@ -1,6 +1,24 @@ +/* + * Copyright 2024 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.springframework.batch.core.step; +import java.time.LocalDateTime; + import org.junit.jupiter.api.Test; + import org.springframework.batch.core.ExitStatus; import org.springframework.batch.core.JobExecution; import org.springframework.batch.core.JobInstance; @@ -9,11 +27,6 @@ import org.springframework.batch.core.StepExecution; import org.springframework.batch.core.StepExecutionListener; import org.springframework.batch.core.repository.JobRepository; -import java.time.LocalDateTime; -import java.util.ArrayList; -import java.util.List; - -import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.mockito.Mockito.mock; @@ -22,36 +35,42 @@ import static org.mockito.Mockito.mock; */ class AbstractStepTests { - private AbstractStep tested; + @Test + void testEndTimeInListener() throws Exception { + // given + StepExecution execution = new StepExecution("step", + new JobExecution(new JobInstance(1L, "job"), new JobParameters())); + AbstractStep tested = new AbstractStep() { + @Override + protected void doExecute(StepExecution stepExecution) { + } + }; + JobRepository jobRepository = mock(); + Listener stepListener = new Listener(); + tested.setStepExecutionListeners(new StepExecutionListener[] { stepListener }); + tested.setJobRepository(jobRepository); + // when + tested.execute(execution); - StepExecution execution = new StepExecution("foo", - new JobExecution(new JobInstance(1L, "bar"), new JobParameters())); + // then + assertNotNull(stepListener.getStepEndTime()); + } - @Test - void testSetEndTime() throws Exception { - tested = new AbstractStep() { - @Override - protected void doExecute(StepExecution stepExecution) { - } - }; + static class Listener implements StepExecutionListener { - JobRepository jobRepository = mock(); + private LocalDateTime stepEndTime; - final List stepEndTime = new ArrayList<>(); + @Override + public ExitStatus afterStep(StepExecution stepExecution) { + this.stepEndTime = stepExecution.getEndTime(); + return ExitStatus.COMPLETED; + } - tested.setStepExecutionListeners(new StepExecutionListener[] { new StepExecutionListener() { - @Override - public ExitStatus afterStep(StepExecution stepExecution) { - stepEndTime.add(stepExecution.getEndTime()); - return ExitStatus.COMPLETED; - } - } }); + public LocalDateTime getStepEndTime() { + return this.stepEndTime; + } - tested.setJobRepository(jobRepository); - tested.execute(execution); + } - assertEquals(1, stepEndTime.size()); - assertNotNull(stepEndTime.get(0)); - } }