https://jojoldu.tistory.com/455?category=902551
이동욱 님의 스프링 배치 가이드를 보고 공부 중 정리한 내용입니다.
위의 동욱님의 Spring Batch 테스트를 따라 하던 도중 Jpa가 정상적으로 동작하지 않는 문제가 발생 해결 방법을 찾고 공유 드립니다.
정확한 방법이 아닐 수도 있습니다. 혹시 더 좋은 방법을 아신다면 알려주세요!!!!!
예제 테스트 코드입니다. 간단한 기능을 사용하여 jpa 배치를 구성하였습니다.
@SpringBatchTest
@SpringBootTest(classes = {TestBatchConfig.class, JpaItemWriterJobConfiguration.class})
class JpaItemWriterJobConfigurationTest {
@Autowired
PayRepository payRepository;
@Autowired
Pay2Repository pay2Repository;
@Autowired
private JobLauncherTestUtils jobLauncherTestUtils;
@BeforeEach
void beforeEach() throws SQLException {
}
@AfterEach
public void tearDown() throws Exception {
payRepository.deleteAllInBatch();
pay2Repository.deleteAllInBatch();
}
@Test
void batchSampleTest() throws Exception {
//given
payRepository.save(new Pay(9000L, "test4", LocalDateTime.now()));
payRepository.save(new Pay(10000L, "test1", LocalDateTime.now()));
payRepository.save(new Pay(20000L, "test2", LocalDateTime.now()));
payRepository.save(new Pay(30000L, "test3", LocalDateTime.now()));
//when
JobExecution jobExecution = jobLauncherTestUtils.launchJob();
//then
ExitStatus exitStatus = jobExecution.getExitStatus();
assertThat(exitStatus.getExitCode()).isEqualTo(COMPLETED.toString());
List<Pay2> findList = pay2Repository.findAll();
assertThat(findList.size()).isEqualTo(3);
assertThat(findList.get(0).getAmount()).isEqualTo(10100L);
assertThat(findList.get(1).getAmount()).isEqualTo(20100L);
assertThat(findList.get(2).getAmount()).isEqualTo(30100L);
}
}
@Slf4j
@RequiredArgsConstructor
@Configuration
public class JpaItemWriterJobConfiguration {
private final JobBuilderFactory jobBuilderFactory;
private final StepBuilderFactory stepBuilderFactory;
private final EntityManagerFactory entityManagerFactory;
private static final int chunkSize = 10;
@Bean
public Job jpaItemWriterJob() {
return jobBuilderFactory.get("jpaItemWriterJob")
.start(jpaItemWriterStep())
.build();
}
@Bean
public Step jpaItemWriterStep() {
return stepBuilderFactory.get("jpaItemWriterStep")
.<Pay, Pay2>chunk(chunkSize)
.reader(jpaItemWriterReader())
.processor(jpaItemProcessor())
.writer(jpaItemWriter())
.build();
}
@Bean
public JpaPagingItemReader<Pay> jpaItemWriterReader() {
return new JpaPagingItemReaderBuilder<Pay>()
.name("jpaItemWriterReader")
.entityManagerFactory(entityManagerFactory)
.pageSize(chunkSize)
.queryString("SELECT p FROM Pay p where amount > 9000")
.build();
}
@Bean
public ItemProcessor<Pay, Pay2> jpaItemProcessor() {
return pay -> new Pay2(pay.getAmount()+100, pay.getTxName(), pay.getTxDateTime());
}
@Bean
public JpaItemWriter<Pay2> jpaItemWriter() {
JpaItemWriter<Pay2> jpaItemWriter = new JpaItemWriter<>();
jpaItemWriter.setEntityManagerFactory(entityManagerFactory);
return jpaItemWriter;
}
}
@Configuration
@EnableAutoConfiguration
@EnableBatchProcessing
@EntityScan("com.miz.batch.model")
@EnableJpaRepositories("com.miz.batch.repository")
@EnableTransactionManagement
public class TestBatchConfig {
/**
* @EntityScan("com.miz.batch.model")
* @EnableJpaRepositories("com.miz.batch.repository")
* @EnableTransactionManagement
* 위의 어노테이션을 추가해줌으로써 jpa 설정을 추가해준다
* 테스트코드에서 정상 동작한다.
*/
}
감사합니다... ㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠㅠ 이걸로 하루종일 잡고 있었는데 덕분입니다. ㅠㅠㅠㅠㅠ 복받으세요 !!!