0
votes

I'm looking for some general opinions and advice on testing a Spring batch step and step execution.

My basic step reads in from an api, processes into an entity object and then writes to a DB. I have tested the happy path, that the step completes successfully. What I now want to do is test the exception handling when data is missing at the processor stage. I could test the processor class in isolation, but I'd rather test the step as a whole to ensure the process failure is reflected correctly at step/job level.

I've read the spring batch testing guidelines and if I'm honest, I'm slightly lost within it. Is it possible to use StepScopeTestUtils.doInStepScope or updating the StepExecution to test this scenario? Ideally I'd force the reader to return faulty data before the processor kicks in.

Any advice would be greatly appreciated.

1

1 Answers

0
votes

The best approach depends on the scope of your test. Reading a little between the lines here, I assume you are using a Spring IT, setting up a Spring context and using the JobLauncherTestUtils to start a job or a step.

I think the easiest way is replace one of your beans with a mock that triggers the error scenario. Using Mockito, this can be done by adding something like this to your test-configuration.

@Bean
public ReaderDataRepository dataApi(){
    return mock(ReaderDataRepository.class);
}

This bean then overrides the actual implementation. In the test setup you can then configure this mock very explicitly.

@Autowired
private ReaderDataRepository mockedRepository;

@Before
public void setUp() {
    when(mockedRepository.getData()).thenReturn(faultyData())
}

This involves very little manipulation of Spring 'magic' and very explicitly defines the error from within the test.