Refine contribution #4522

* Update tests
This commit is contained in:
Mahmoud Ben Hassine
2024-04-18 16:23:06 +02:00
parent df5e8e1686
commit 6b79e2c1f7

View File

@@ -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<LocalDateTime> 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));
}
}