|
| 1 | +package com.example.batch; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.mockito.ArgumentMatchers.any; |
| 5 | +import static org.mockito.Mockito.mock; |
| 6 | +import static org.mockito.Mockito.verify; |
| 7 | +import static org.mockito.Mockito.when; |
| 8 | + |
| 9 | +import com.example.batch.enums.BatchResult; |
| 10 | +import com.example.batch.logic.CsvToDbLogic; |
| 11 | +import com.example.batch.logic.DbToCsvLogic; |
| 12 | +import com.example.batch.service.CsvToDbService; |
| 13 | +import com.example.batch.service.DbToCsvService; |
| 14 | +import org.junit.jupiter.api.BeforeEach; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | +import org.springframework.batch.core.ExitStatus; |
| 17 | +import org.springframework.batch.core.Job; |
| 18 | +import org.springframework.batch.core.JobExecution; |
| 19 | +import org.springframework.batch.test.JobLauncherTestUtils; |
| 20 | +import org.springframework.batch.test.JobRepositoryTestUtils; |
| 21 | +import org.springframework.batch.test.context.SpringBatchTest; |
| 22 | +import org.springframework.beans.factory.annotation.Autowired; |
| 23 | +import org.springframework.beans.factory.annotation.Qualifier; |
| 24 | +import org.springframework.boot.test.context.SpringBootTest; |
| 25 | +import org.springframework.test.context.TestPropertySource; |
| 26 | +import org.springframework.test.util.ReflectionTestUtils; |
| 27 | + |
| 28 | +@TestPropertySource( |
| 29 | + properties = { |
| 30 | + "spring.batch.job.enabled=false", |
| 31 | + "spring.datasource.mysqlmain.url=jdbc:mysql://localhost:3306/sampledb", |
| 32 | + "spring.datasource.mysqlmain.username=sampleuser", |
| 33 | + "spring.datasource.mysqlmain.password=samplepassword" |
| 34 | + }) |
| 35 | +@SpringBootTest |
| 36 | +@SpringBatchTest |
| 37 | +class BatchJobTest { |
| 38 | + |
| 39 | + @Autowired private JobLauncherTestUtils jobLauncherTestUtils; |
| 40 | + |
| 41 | + @Autowired private JobRepositoryTestUtils jobRepositoryTestUtils; |
| 42 | + |
| 43 | + @Autowired |
| 44 | + @Qualifier("createDbToCsvJob") |
| 45 | + private Job dbToCsvJob; |
| 46 | + |
| 47 | + @Autowired |
| 48 | + @Qualifier("createCsvToDbJob") |
| 49 | + private Job csvToDbJob; |
| 50 | + |
| 51 | + @Autowired private DbToCsvLogic dbToCsvLogic; |
| 52 | + |
| 53 | + @Autowired private CsvToDbLogic csvToDbLogic; |
| 54 | + |
| 55 | + private DbToCsvService dbToCsvService; |
| 56 | + private CsvToDbService csvToDbService; |
| 57 | + |
| 58 | + @BeforeEach |
| 59 | + void setUp() { |
| 60 | + dbToCsvService = mock(DbToCsvService.class); |
| 61 | + csvToDbService = mock(CsvToDbService.class); |
| 62 | + |
| 63 | + ReflectionTestUtils.setField(dbToCsvLogic, "dbToCsvService", dbToCsvService); |
| 64 | + ReflectionTestUtils.setField(csvToDbLogic, "csvToDbService", csvToDbService); |
| 65 | + |
| 66 | + jobRepositoryTestUtils.removeJobExecutions(); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void testDbToCsvJobSuccess() throws Exception { |
| 71 | + System.setProperty("spring.batch.job.name", "DB_TO_CSV"); |
| 72 | + jobLauncherTestUtils.setJob(dbToCsvJob); |
| 73 | + |
| 74 | + when(dbToCsvService.execute(any(), any())).thenReturn(BatchResult.SUCCESS); |
| 75 | + JobExecution jobExecution = jobLauncherTestUtils.launchJob(); |
| 76 | + assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus()); |
| 77 | + verify(dbToCsvService).execute(any(), any()); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + void testCsvToDbJobSuccess() throws Exception { |
| 82 | + System.setProperty("spring.batch.job.name", "CSV_TO_DB"); |
| 83 | + jobLauncherTestUtils.setJob(csvToDbJob); |
| 84 | + |
| 85 | + when(csvToDbService.execute(any())).thenReturn(BatchResult.SUCCESS); |
| 86 | + JobExecution jobExecution = jobLauncherTestUtils.launchJob(); |
| 87 | + assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus()); |
| 88 | + verify(csvToDbService).execute(any()); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + void testDbToCsvJobFailure() throws Exception { |
| 93 | + System.setProperty("spring.batch.job.name", "DB_TO_CSV"); |
| 94 | + jobLauncherTestUtils.setJob(dbToCsvJob); |
| 95 | + |
| 96 | + when(dbToCsvService.execute(any(), any())).thenThrow(new RuntimeException("Test exception")); |
| 97 | + JobExecution jobExecution = jobLauncherTestUtils.launchJob(); |
| 98 | + assertEquals(ExitStatus.FAILED, jobExecution.getExitStatus()); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + void testCsvToDbJobFailure() throws Exception { |
| 103 | + System.setProperty("spring.batch.job.name", "CSV_TO_DB"); |
| 104 | + jobLauncherTestUtils.setJob(csvToDbJob); |
| 105 | + |
| 106 | + when(csvToDbService.execute(any())).thenThrow(new RuntimeException("Test exception")); |
| 107 | + JobExecution jobExecution = jobLauncherTestUtils.launchJob(); |
| 108 | + assertEquals(ExitStatus.FAILED, jobExecution.getExitStatus()); |
| 109 | + } |
| 110 | +} |
0 commit comments