mzlle
mzlle

Reputation: 31

Unit test spring batch configuration using spring boot

I'm working with spring batch 4 with annotations and i have a java class for configuration like this :

@Autowired
public JobBuilderFactory jobBuilderFactory;

@Autowired
public StepBuilderFactory stepBuilderFactory;

@Bean
public Job firstJob() {
    return jobBuilderFactory.get("firstJob").incrementer(new RunIdIncrementer())
            .flow(firstStep()).end().build();
}

private Step firstStep () {
    return stepBuilderFactory.get("firstStep")
            .<Student, Student>chunk(100).reader(customReader())
            .processor(customProcessor()).writer(customWriter()).build();
}

@Bean
ItemReader<Student> CustomReader () {
    return new CustomReader ("students.xml");
}

@Bean
public CustomProcessor customProcessor () {
    return new CustomProcessor ();
}

@Bean
public CustomWriter customWriter () {
    return new CustomWriter ();
}

there is any way to test this class of configuration ? i can't found a way in spring documentation. CustomReader, CustomProcessor... already tested

Upvotes: 1

Views: 4489

Answers (1)

ndrone
ndrone

Reputation: 3572

I'm not sure what version of spring boot you are using. But the way I've been testing configurations is with @SpringBootTest

@SpringBootTest(classes = {YourConfiguration.class}
YourConfigurationTest
{
    @Autowired
    private ApplicationContext context;

    @Test
    public void testBeans()
    {
         Job job = context.getBean(Job.class);
         Assert.notNull(job);
    }
}

Upvotes: 1

Related Questions