|
| 1 | +package com.springbatch.excel.tutorial; |
| 2 | +import com.springbatch.excel.tutorial.batch.BatchConfiguration; |
| 3 | +import com.springbatch.excel.tutorial.batch.listeners.JobCompletionListener; |
| 4 | +import com.springbatch.excel.tutorial.domain.Employee; |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 7 | +import org.springframework.batch.core.Job; |
| 8 | +import org.springframework.batch.core.JobExecution; |
| 9 | +import org.springframework.batch.core.JobParameter; |
| 10 | +import org.springframework.batch.core.JobParameters; |
| 11 | +import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; |
| 12 | +import org.springframework.batch.core.launch.JobLauncher; |
| 13 | +import org.springframework.beans.factory.annotation.Autowired; |
| 14 | +import org.springframework.boot.test.context.SpringBootTest; |
| 15 | +import org.springframework.context.annotation.ComponentScan; |
| 16 | +import org.springframework.data.mongodb.core.MongoTemplate; |
| 17 | +import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 18 | + |
| 19 | +import java.util.*; |
| 20 | + |
| 21 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +@ExtendWith(SpringExtension.class) |
| 26 | +@EnableBatchProcessing |
| 27 | +@ComponentScan(basePackages = "com.springbatch.excel.tutorial") |
| 28 | +@SpringBootTest(classes = {BatchConfiguration.class, JobCompletionListener.class}) |
| 29 | +class BatchIntegrationTest { |
| 30 | + |
| 31 | + @Autowired |
| 32 | + private JobLauncher jobLauncher; |
| 33 | + |
| 34 | + @Autowired |
| 35 | + private Job jsonFileProcessingJob; |
| 36 | + |
| 37 | + @Autowired |
| 38 | + private MongoTemplate mongoTemplate; |
| 39 | + |
| 40 | + @Test |
| 41 | + void testBatchJob() throws Exception { |
| 42 | + String absolutePath = Objects.requireNonNull(getClass().getResource("/data/processing/employee.xlsx")).getPath(); |
| 43 | + |
| 44 | + // Créez les paramètres du job |
| 45 | + Map<String, JobParameter> jobParameters = new HashMap<>(); |
| 46 | + jobParameters.put("filePath", new JobParameter(absolutePath)); |
| 47 | + jobParameters.put("currentTime", new JobParameter(new Date())); |
| 48 | + |
| 49 | + // Lancez le job avec les paramètres |
| 50 | + JobExecution jobExecution = jobLauncher.run(jsonFileProcessingJob,new JobParameters(jobParameters)); |
| 51 | + |
| 52 | + // Vérifiez que le job s'est terminé avec succès |
| 53 | + assertThat(jobExecution.getExitStatus().getExitCode()).isEqualTo("COMPLETED"); |
| 54 | + |
| 55 | + List<Employee> couponOffers = mongoTemplate.findAll(Employee.class); |
| 56 | + assertThat(couponOffers.size()).isEqualTo(100); |
| 57 | + |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | + |
0 commit comments