To few helper to test JBatch batches?


Recently BatchEE (Apache implementation of JBatch based on the Reference Implementation) got a test module. This is still “experimental” and can change but here is the first proposal.

JBatch is designed to work in JSE so it is quite easy to start a batch:

BatchRuntime.getJobOperator().start("my-batch-name", myBatchParameters);

That’s great but start is asynchronous so then you need to wait the end of the “test” batch. The hackers will immediately think to set to BatchEE a synchronous ExecutorService. It would work but is not portable and can prevent to test some advanced batches (with partitions for instance).

So first BatchEE introduces a org.apache.batchee.test.SynchronousJobOperator. It makes start/restart/stop synchronous. So now you can do:

final JobOperator operator = new SynchronousJobOperator();
operator.start("my-batch", myConfig);
assertEquals(BatchStatus.COMPLETED, operator.getJobExecution(id).getBatchStatus());

This is great but we can go a bit further. That’s the goal of JobLauncher and where the most experimentations can be done.

First it offers you a shortcut to start a job and get its JobExecution:

final JobExecution execution = JobLauncher.start("my-batch", batchConfig);
assertEquals(BatchStatus.COMPLETED, execution.getBatchStatus());

Another interesting way to do so is to specialize a JobLauncher to a batch:

final JobExecution execution = new JobLauncher("sleep").start(anotherConfig);
assertEquals(BatchStatus.COMPLETED, execution.getBatchStatus());

This way you can run multiple times the same batch just changing properties.

And as a bonus it also offers you a quick way to retrieve batch step executions:

final JobLauncher launcher = new JobLauncher("one-OK-step-batch");
launcher.start(new Properties());
final List<StepExecution> executions = launcher.getLastStepExecutions();
assertEquals(1, executions.size());
assertEquals("OK", executions.iterator().next().getExitStatus());

Hope it is useful!

Merry Xmas and happy new year!

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s