|
| 1 | +/* |
| 2 | + * Copyright 2025-present the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.batch.core.step.item; |
| 17 | + |
| 18 | +import org.junit.jupiter.api.Test; |
| 19 | + |
| 20 | +import org.springframework.batch.core.job.JobExecution; |
| 21 | +import org.springframework.batch.core.job.JobInstance; |
| 22 | +import org.springframework.batch.core.job.parameters.JobParameters; |
| 23 | +import org.springframework.batch.core.repository.JobRepository; |
| 24 | +import org.springframework.batch.core.repository.support.ResourcelessJobRepository; |
| 25 | +import org.springframework.batch.core.step.StepExecution; |
| 26 | +import org.springframework.batch.infrastructure.item.ItemReader; |
| 27 | +import org.springframework.batch.infrastructure.item.ItemWriter; |
| 28 | + |
| 29 | +import static org.mockito.Mockito.mock; |
| 30 | +import static org.mockito.Mockito.times; |
| 31 | +import static org.mockito.Mockito.verify; |
| 32 | +import static org.mockito.Mockito.when; |
| 33 | + |
| 34 | +/** |
| 35 | + * @author Mahmoud Ben Hassine |
| 36 | + */ |
| 37 | +public class ChunkOrientedStepTests { |
| 38 | + |
| 39 | + @Test |
| 40 | + void testReadNoMoreThanAvailableItems() throws Exception { |
| 41 | + // given |
| 42 | + ItemReader<String> reader = mock(); |
| 43 | + ItemWriter<String> writer = chunk -> { |
| 44 | + }; |
| 45 | + JobRepository jobRepository = new ResourcelessJobRepository(); |
| 46 | + when(reader.read()).thenReturn("1", "2", "3", "4", "5", null); |
| 47 | + ChunkOrientedStep<String, String> step = new ChunkOrientedStep<>("step", 10, reader, writer, jobRepository); |
| 48 | + step.afterPropertiesSet(); |
| 49 | + JobInstance jobInstance = new JobInstance(1L, "job"); |
| 50 | + JobExecution jobExecution = new JobExecution(1L, jobInstance, new JobParameters()); |
| 51 | + StepExecution stepExecution = new StepExecution(1L, "step", jobExecution); |
| 52 | + |
| 53 | + // when |
| 54 | + step.execute(stepExecution); |
| 55 | + |
| 56 | + // then |
| 57 | + verify(reader, times(6)).read(); |
| 58 | + } |
| 59 | + |
| 60 | +} |
0 commit comments