Exemple de test d’unité Spring Batch

Exemple de test unitaire Spring Batch

Dans ce didacticiel, nous allons vous montrer comment tester les travaux par lots Spring avec les frameworks jUnit et TestNG. Pour tester un travail par lots, déclarespring-batch-test.jar,@autowired lesJobLauncherTestUtils, lancez le travail ou l'étape et confirmez l'état d'exécution.

1. Dépendances des tests unitaires

Pour un test unitaire Spring batch, déclare les dépendances suivantes:

pom.xml

    
    
        org.springframework.batch
        spring-batch-core
        2.2.0.RELEASE
    
    
        org.springframework.batch
        spring-batch-infrastructure
        2.2.0.RELEASE
    

    
    
        org.springframework.batch
        spring-batch-test
        2.2.0.RELEASE
    

    
    
        junit
        junit
        4.11
        test
    

    
    
        org.testng
        testng
        6.8.5
        test
    

2. Emplois Spring Batch

Un travail simple, plus tard test unitaire de l'état d'exécution.

spring-batch-job.xml

    
    
    
        
        
        
        
    
    

3. Exemples jUnit

Lance un travail et confirme l'état de l'exécution.

AppTest.java

package com.example;

import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
    "classpath:spring/batch/jobs/spring-batch-job.xml",
    "classpath:spring/batch/config/context.xml",
    "classpath:spring/batch/config/test-context.xml"})
public class AppTest {

    @Autowired
    private JobLauncherTestUtils jobLauncherTestUtils;

    @Test
    public void launchJob() throws Exception {

    //testing a job
        JobExecution jobExecution = jobLauncherTestUtils.launchJob();

    //Testing a individual step
        //JobExecution jobExecution = jobLauncherTestUtils.launchStep("step1");

        assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());

    }
}

P.S Assume context.xml is declared all the require Spring batch core components, like jobRepository and etc.

CeJobLauncherTestUtils doit être déclaré manuellement.

test-context.xml



    
    

4. Exemples TestNG

Exemple équivalent dans le framework TestNG.

AppTest2.java

package com.example;

import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.Assert;
import org.testng.annotations.Test;

@ContextConfiguration(locations = {
    "classpath:spring/batch/jobs/spring-batch-job.xml",
    "classpath:spring/batch/config/context.xml",
    "classpath:spring/batch/config/test-context.xml"})
public class AppTest2 extends AbstractTestNGSpringContextTests {

    @Autowired
    private JobLauncherTestUtils jobLauncherTestUtils;


    @Test
    public void launchJob() throws Exception {

        JobExecution jobExecution = jobLauncherTestUtils.launchJob();
        Assert.assertEquals(jobExecution.getStatus(), BatchStatus.COMPLETED);

    }
}

Terminé.

Télécharger le code source

Téléchargez-le -SpringBatch-UnitTest-Example.zip (83 Ko)