Skip to content

Commit

Permalink
Refine contribution #4522
Browse files Browse the repository at this point in the history
* Update tests
  • Loading branch information
fmbenhassine committed Apr 18, 2024
1 parent df5e8e1 commit 6b79e2c
Showing 1 changed file with 48 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -9,11 +27,6 @@
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;

Expand All @@ -22,36 +35,42 @@
*/
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));
}
}

0 comments on commit 6b79e2c

Please sign in to comment.