Skip to content

Commit 1e553e2

Browse files
committed
test: add batch integration test
1 parent 92289f6 commit 1e553e2

File tree

7 files changed

+91
-5
lines changed

7 files changed

+91
-5
lines changed

pom.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,6 @@
5050
<artifactId>spring-boot-starter-test</artifactId>
5151
<scope>test</scope>
5252
</dependency>
53-
<dependency>
54-
<groupId>org.springframework.batch</groupId>
55-
<artifactId>spring-batch-test</artifactId>
56-
<scope>test</scope>
57-
</dependency>
5853

5954
</dependencies>
6055

src/main/java/com/springbatch/excel/tutorial/batch/BatchConfiguration.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,18 @@
1212
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
1313
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
1414
import org.springframework.batch.core.job.CompositeJobParametersValidator;
15+
import org.springframework.batch.core.job.builder.JobBuilder;
1516
import org.springframework.batch.core.launch.support.RunIdIncrementer;
17+
import org.springframework.batch.core.repository.JobRepository;
18+
import org.springframework.batch.core.step.builder.StepBuilder;
1619
import org.springframework.batch.item.ItemProcessor;
1720
import org.springframework.batch.item.ItemReader;
1821
import org.springframework.batch.item.data.MongoItemWriter;
1922
import org.springframework.batch.item.data.builder.MongoItemWriterBuilder;
2023
import org.springframework.context.annotation.Bean;
2124
import org.springframework.context.annotation.Configuration;
2225
import org.springframework.data.mongodb.core.MongoTemplate;
26+
import org.springframework.transaction.PlatformTransactionManager;
2327

2428
import java.util.Collections;
2529

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+

src/test/resources/application.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
spring:
2+
data:
3+
mongodb:
4+
host: localhost
5+
port: 27017
6+
database: spring_batch_excel_mongodb_db
7+
# username: admin
8+
# password: pass
9+
auto-index-creation: true
10+
11+
jackson:
12+
default-property-inclusion: NON_NULL
13+
#disabled job run at startup
14+
batch:
15+
job:
16+
enabled: false
17+
18+
# Enable Logging mongo queries
19+
logging:
20+
level:
21+
org.springframework.data.mongodb.core.MongoTemplate: DEBUG
22+
23+
employee:
24+
excel:
25+
processingfolder: data/processing/
26+
resultsfolder: data/results/

src/test/resources/data/processing/employee.txt

Whitespace-only changes.
16.1 KB
Binary file not shown.

src/test/resources/data/results/readme.txt

Whitespace-only changes.

0 commit comments

Comments
 (0)