File tree Expand file tree Collapse file tree 4 files changed +46
-0
lines changed
main/java/org/springframework/batch/core
test/java/org/springframework/batch/core Expand file tree Collapse file tree 4 files changed +46
-0
lines changed Original file line number Diff line number Diff line change @@ -161,6 +161,9 @@ public B listener(Object listener) {
161161 factory .setDelegate (listener );
162162 properties .addJobExecutionListener ((JobExecutionListener ) factory .getObject ());
163163 }
164+ else {
165+ throw new IllegalArgumentException ("Missing @BeforeJob or @AfterJob annotations on Listener." );
166+ }
164167
165168 @ SuppressWarnings ("unchecked" )
166169 B result = (B ) this ;
Original file line number Diff line number Diff line change @@ -125,6 +125,9 @@ public B listener(Object listener) {
125125 factory .setDelegate (listener );
126126 properties .addStepExecutionListener ((StepExecutionListener ) factory .getObject ());
127127 }
128+ else {
129+ throw new IllegalArgumentException ("Missing @BeforeStep or @AfterStep annotations on Listener." );
130+ }
128131
129132 return self ();
130133 }
Original file line number Diff line number Diff line change 1919
2020import org .junit .jupiter .api .Test ;
2121
22+ import org .mockito .Mockito ;
2223import org .springframework .batch .core .ExitStatus ;
2324import org .springframework .batch .core .job .Job ;
2425import org .springframework .batch .core .job .JobExecution ;
4041import org .springframework .transaction .PlatformTransactionManager ;
4142
4243import static org .junit .jupiter .api .Assertions .assertEquals ;
44+ import static org .junit .jupiter .api .Assertions .assertThrows ;
4345
4446/**
4547 * @author Mahmoud Ben Hassine
@@ -65,6 +67,16 @@ void testListeners() throws Exception {
6567
6668 }
6769
70+ @ Test
71+ void testInvalidListener () {
72+ assertThrows (IllegalArgumentException .class ,
73+ () -> new JobBuilder ("job" , Mockito .mock ()).listener (new InvalidListener ())
74+ .start (new StepBuilder ("step" , Mockito .mock ())
75+ .tasklet ((contribution , chunkContext ) -> RepeatStatus .FINISHED , Mockito .mock ())
76+ .build ())
77+ .build ());
78+ }
79+
6880 @ Configuration
6981 @ EnableBatchProcessing
7082 static class MyJobConfiguration {
@@ -130,4 +142,14 @@ public void afterJob(JobExecution jobExecution) {
130142
131143 }
132144
145+ public static class InvalidListener {
146+
147+ public void beforeStep () {
148+ }
149+
150+ public void afterStep () {
151+ }
152+
153+ }
154+
133155}
Original file line number Diff line number Diff line change 6060import org .springframework .transaction .PlatformTransactionManager ;
6161
6262import static org .junit .jupiter .api .Assertions .assertEquals ;
63+ import static org .junit .jupiter .api .Assertions .assertThrows ;
6364
6465/**
6566 * @author Dave Syer
@@ -117,6 +118,13 @@ void testListeners() throws Exception {
117118 assertEquals (1 , AnnotationBasedStepExecutionListener .afterChunkCount );
118119 }
119120
121+ @ Test
122+ void testMissingAnnotationsForListeners () {
123+ assertThrows (IllegalArgumentException .class ,
124+ () -> new StepBuilder ("step" , jobRepository ).listener (new InvalidListener ())
125+ .tasklet ((contribution , chunkContext ) -> null , transactionManager ));
126+ }
127+
120128 @ Test
121129 void testAnnotationBasedChunkListenerForTaskletStep () throws Exception {
122130 TaskletStepBuilder builder = new StepBuilder ("step" , jobRepository )
@@ -465,4 +473,14 @@ public void afterChunkError() {
465473
466474 }
467475
476+ public static class InvalidListener {
477+
478+ public void beforeStep () {
479+ }
480+
481+ public void afterStep () {
482+ }
483+
484+ }
485+
468486}
You can’t perform that action at this time.
0 commit comments