4

I'm having an application where i'm using spring batch. I want to write a test case which can test the batch job end to end. I have been exploring various options for the same. I checked if concordion test cases can be useful but i'm not sure if it's ideal way to test spring-batch jobs. So far i think integration test case should fit the best to my case. I would like to know what should be the ideal approach to test my scenario.

<batch:job id="batch-job">

    <batch:step id="cleanupData" next="populateExchRates">
        <batch:tasklet ref="dataCleanupTasklet" />
    </batch:step>

    <batch:step id="populateExchRates" next="populateCache">
        <batch:tasklet ref="populateExchRatesDBTasklet" />
    </batch:step>       

     <batch:step id="populateCache" next="ExternalDbQuery">
        <batch:tasklet ref="populateFxRatesCacheTasklet" />
    </batch:step>

    <batch:step id="ExternalDbQuery" next="...">
        <batch:tasklet ref="ExternalDBQueryTasklet" />
    </batch:step>

    ...
</batch:job>

We have batch job defined as above, there are more than 20 steps which includes interface with external systems(>5 such steps), there are step readers etc.

While developing Integration test cases i was considering using spring-batch-test API along with Mockito so that I can mock steps involving external systems calls. Using this approach i'll have to create the mock objects in the spring configuration itself(Injecting Mockito mocks into a Spring bean). What i'm not sure about is how i'll mock the method calls for the mocked beans if i configure them using spring as below.

<bean id="dao" class="org.mockito.Mockito" factory-method="mock"> 
      <constructor-arg value="com.package.Dao" /> 
</bean>

I'm not sure if i could explain my scenario clearly or not. Please suggest if you have any other better opinion for testing E2E flow for spring-batch job and also if you can provide any clarity on the above approach that would be helpful.

Community
  • 1
  • 1
ATR
  • 2,160
  • 4
  • 22
  • 43

1 Answers1

2

Your approach seems valid to me. I've used a similar setup many times. Just setup a Spring context from your test and use the JobLauncherTestUtils to work with your jobs.

One thing to consider is what to mock. If multiple consecutive steps read and write the same data from the same datasource, mocking those out, might lead to a lot of mock configuration in your test. Perhaps it is easier then to setup an in memory database for those steps. That depends on the details of your implementation.

Concerning the setup of your mocks... You can simply autowire your mocks into the test.

@Autowired
private Dao mockedDao;

Then you can simply configure that mock as you would any mock.

when(dao.findById(any())).thenReturn(something);
Maarten
  • 196
  • 8