2 Haziran 2023 Cuma

SpringBatch JobLauncherTestUtils Sınıfı - Test İçindir

Giriş
Şu satırı dahil ederiz 
import org.springframework.batch.test.JobLauncherTestUtils;
Açıklaması şöyle
Spring Batch provides a test framework that enables developers to write unit tests for batch jobs. The framework includes test helpers that simulate the behavior of the batch job and validate the output of the job.

Örnek
Şöyle yaparız
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBatchTest
@ContextConfiguration(classes = { BatchTestConfig.class, SimpleJobConfig.class })
public class SimpleJobTest {

  @Autowired
  private JobLauncherTestUtils jobLauncherTestUtils;

  @Autowired
  private DataSource dataSource;

  @Test
  public void testSimpleJob() throws Exception {
    JobExecution jobExecution = jobLauncherTestUtils.launchJob();

    // Assert the job execution status
    assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());

    // Assert the number of records processed
    JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    assertEquals(3, jdbcTemplate
      .queryForObject("SELECT COUNT(*) FROM people", Integer.class)
      .intValue());
  }
}
Açıklaması şöyle
In this example, we are using the `JobLauncherTestUtils` helper class to launch the job and verify its execution status. We are also using the JdbcTemplate to query the database and verify the number of records processed.

Note that we are using the `@SpringBatchTest` annotation to enable Spring Batch test support, and we are also providing the configuration classes for the batch job and the test environment using the `@ContextConfiguration` annotation.

Hiç yorum yok:

Yorum Gönder