diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/step/tasklet/CallableTaskletAdapter.java b/spring-batch-core/src/main/java/org/springframework/batch/core/step/tasklet/CallableTaskletAdapter.java index ccdf997917..95da816b1f 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/step/tasklet/CallableTaskletAdapter.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/step/tasklet/CallableTaskletAdapter.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2022 the original author or authors. + * Copyright 2006-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -34,6 +34,21 @@ public class CallableTaskletAdapter implements Tasklet, InitializingBean { private Callable callable; + /** + * Create a new {@link CallableTaskletAdapter} instance. + */ + public CallableTaskletAdapter() { + } + + /** + * Create a new {@link CallableTaskletAdapter} instance. + * @param callable the {@link Callable} to use + */ + public CallableTaskletAdapter(Callable callable) { + setCallable(callable); + afterPropertiesSet(); + } + /** * Public setter for the {@link Callable}. * @param callable the {@link Callable} to set @@ -48,7 +63,7 @@ public void setCallable(Callable callable) { * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet() */ @Override - public void afterPropertiesSet() throws Exception { + public void afterPropertiesSet() { Assert.state(callable != null, "A Callable is required"); } diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/step/tasklet/CallableTaskletAdapterTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/step/tasklet/CallableTaskletAdapterTests.java index 07f861fb58..9d221c6abc 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/step/tasklet/CallableTaskletAdapterTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/step/tasklet/CallableTaskletAdapterTests.java @@ -23,17 +23,22 @@ class CallableTaskletAdapterTests { - private final CallableTaskletAdapter adapter = new CallableTaskletAdapter(); + @Test + public void testHandleWithConstructor() throws Exception { + CallableTaskletAdapter adapter = new CallableTaskletAdapter(() -> RepeatStatus.FINISHED); + assertEquals(RepeatStatus.FINISHED, adapter.execute(null, null)); + } @Test - void testHandle() throws Exception { + void testHandleWithSetter() throws Exception { + CallableTaskletAdapter adapter = new CallableTaskletAdapter(); adapter.setCallable(() -> RepeatStatus.FINISHED); assertEquals(RepeatStatus.FINISHED, adapter.execute(null, null)); } @Test void testAfterPropertiesSet() { - assertThrows(IllegalStateException.class, adapter::afterPropertiesSet); + assertThrows(IllegalStateException.class, new CallableTaskletAdapter()::afterPropertiesSet); } }