Skip to content

Commit

Permalink
test: add batch integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
Raouf25 committed Nov 22, 2023
1 parent 92289f6 commit d90e76c
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 5 deletions.
5 changes: 0 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,6 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-test</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,18 @@
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.job.CompositeJobParametersValidator;
import org.springframework.batch.core.job.builder.JobBuilder;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.data.MongoItemWriter;
import org.springframework.batch.item.data.builder.MongoItemWriterBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.transaction.PlatformTransactionManager;

import java.util.Collections;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.springbatch.excel.tutorial;
import com.springbatch.excel.tutorial.batch.BatchConfiguration;
import com.springbatch.excel.tutorial.batch.listeners.JobCompletionListener;
import com.springbatch.excel.tutorial.domain.Employee;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameter;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import java.util.*;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;



@ExtendWith(SpringExtension.class)
@EnableBatchProcessing
@ComponentScan(basePackages = "com.springbatch.excel.tutorial")
@SpringBootTest(classes = {BatchConfiguration.class, JobCompletionListener.class})
class BatchIntegrationTest {

@Autowired
private JobLauncher jobLauncher;

@Autowired
private Job jsonFileProcessingJob;

@Autowired
private MongoTemplate mongoTemplate;

@Test
void testBatchJob() throws Exception {
String absolutePath = Objects.requireNonNull(getClass().getResource("/data/processing/employee.xlsx")).getPath();

// Créez les paramètres du job
Map<String, JobParameter> jobParameters = new HashMap<>();
jobParameters.put("filePath", new JobParameter(absolutePath));
jobParameters.put("currentTime", new JobParameter(new Date()));

// Lancez le job avec les paramètres
JobExecution jobExecution = jobLauncher.run(jsonFileProcessingJob,new JobParameters(jobParameters));

// Vérifiez que le job s'est terminé avec succès
assertThat(jobExecution.getExitStatus().getExitCode()).isEqualTo("COMPLETED");

List<Employee> couponOffers = mongoTemplate.findAll(Employee.class);
assertThat(couponOffers.size()).isEqualTo(100);

}
}


26 changes: 26 additions & 0 deletions src/test/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
spring:
data:
mongodb:
host: localhost
port: 27017
database: spring_batch_excel_mongodb_db
# username: admin
# password: pass
auto-index-creation: true

jackson:
default-property-inclusion: NON_NULL
#disabled job run at startup
batch:
job:
enabled: false

# Enable Logging mongo queries
logging:
level:
org.springframework.data.mongodb.core.MongoTemplate: DEBUG

employee:
excel:
processingfolder: data/processing/
resultsfolder: data/results/
Empty file.
Binary file added src/test/resources/data/processing/employee.xlsx
Binary file not shown.
Empty file.

0 comments on commit d90e76c

Please sign in to comment.