|
| 1 | +package com.baeldung.batch; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 7 | +import static org.mockito.Mockito.mock; |
| 8 | +import static org.mockito.Mockito.when; |
| 9 | + |
| 10 | +import java.math.BigDecimal; |
| 11 | +import java.util.Arrays; |
| 12 | + |
| 13 | +import org.junit.jupiter.api.BeforeEach; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | +import org.springframework.batch.core.JobExecution; |
| 16 | +import org.springframework.batch.core.JobParameters; |
| 17 | +import org.springframework.batch.core.StepExecution; |
| 18 | +import org.springframework.batch.item.ExecutionContext; |
| 19 | +import org.springframework.batch.item.ItemReader; |
| 20 | +import org.springframework.batch.item.ItemStreamReader; |
| 21 | +import org.springframework.batch.item.support.CompositeItemReader; |
| 22 | +import org.springframework.beans.factory.annotation.Autowired; |
| 23 | +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
| 24 | +import org.springframework.boot.test.context.SpringBootTest; |
| 25 | +import org.springframework.jdbc.core.JdbcTemplate; |
| 26 | +import org.springframework.test.context.ContextConfiguration; |
| 27 | + |
| 28 | +import com.baeldung.batch.model.Product; |
| 29 | + |
| 30 | +@SpringBootTest |
| 31 | +@EnableAutoConfiguration |
| 32 | +@ContextConfiguration(classes = { CompositeItemReaderConfig.class }) |
| 33 | +public class CompositeItemReaderUnitTest { |
| 34 | + @Autowired |
| 35 | + private CompositeItemReader<Product> compositeReader; |
| 36 | + |
| 37 | + @Autowired |
| 38 | + private JdbcTemplate jdbcTemplate; |
| 39 | + |
| 40 | + @BeforeEach |
| 41 | + public void setUp() { |
| 42 | + jdbcTemplate.update("DELETE FROM products"); |
| 43 | + jdbcTemplate.update("INSERT INTO products (productid, productname, stock, price) VALUES (?, ?, ?, ?)", |
| 44 | + 102, "Banana", 30, 1.49); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void givenTwoReaders_whenRead_thenProcessProductsInOrder() throws Exception { |
| 49 | + StepExecution stepExecution = new StepExecution("testStep", new JobExecution(1L, new JobParameters()), 1L); |
| 50 | + ExecutionContext executionContext = stepExecution.getExecutionContext(); |
| 51 | + compositeReader.open(executionContext); |
| 52 | + |
| 53 | + Product product1 = compositeReader.read(); |
| 54 | + assertNotNull(product1); |
| 55 | + assertEquals(101, product1.getProductId()); |
| 56 | + assertEquals("Apple", product1.getProductName()); |
| 57 | + |
| 58 | + Product product2 = compositeReader.read(); |
| 59 | + assertNotNull(product2); |
| 60 | + assertEquals(102, product2.getProductId()); |
| 61 | + assertEquals("Banana", product2.getProductName()); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void givenMultipleReader_whenOneReaderReturnNull_thenProcessDataFromNextReader() throws Exception { |
| 66 | + ItemStreamReader<Product> emptyReader = mock(ItemStreamReader.class); |
| 67 | + when(emptyReader.read()).thenReturn(null); |
| 68 | + |
| 69 | + ItemStreamReader<Product> validReader = mock(ItemStreamReader.class); |
| 70 | + when(validReader.read()).thenReturn(new Product(103L, "Cherry", 20, BigDecimal.valueOf(2.99)), null); |
| 71 | + |
| 72 | + CompositeItemReader<Product> compositeReader = new CompositeItemReader<>(Arrays.asList(emptyReader, validReader)); |
| 73 | + |
| 74 | + Product product = compositeReader.read(); |
| 75 | + assertNotNull(product); |
| 76 | + assertEquals(103, product.getProductId()); |
| 77 | + assertEquals("Cherry", product.getProductName()); |
| 78 | + } |
| 79 | + |
| 80 | + |
| 81 | + @Test |
| 82 | + public void givenEmptyReaders_whenRead_thenReturnNull() throws Exception { |
| 83 | + ItemStreamReader<Product> emptyReader = mock(ItemStreamReader.class); |
| 84 | + when(emptyReader.read()).thenReturn(null); |
| 85 | + |
| 86 | + CompositeItemReader<Product> compositeReader = new CompositeItemReader<>(Arrays.asList(emptyReader, emptyReader)); |
| 87 | + |
| 88 | + Product product = compositeReader.read(); |
| 89 | + assertNull(product); |
| 90 | + } |
| 91 | +} |
0 commit comments