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) {
161
161
factory .setDelegate (listener );
162
162
properties .addJobExecutionListener ((JobExecutionListener ) factory .getObject ());
163
163
}
164
+ else {
165
+ throw new IllegalArgumentException ("Missing @BeforeJob or @AfterJob annotations on Listener." );
166
+ }
164
167
165
168
@ SuppressWarnings ("unchecked" )
166
169
B result = (B ) this ;
Original file line number Diff line number Diff line change @@ -125,6 +125,9 @@ public B listener(Object listener) {
125
125
factory .setDelegate (listener );
126
126
properties .addStepExecutionListener ((StepExecutionListener ) factory .getObject ());
127
127
}
128
+ else {
129
+ throw new IllegalArgumentException ("Missing @BeforeStep or @AfterStep annotations on Listener." );
130
+ }
128
131
129
132
return self ();
130
133
}
Original file line number Diff line number Diff line change 19
19
20
20
import org .junit .jupiter .api .Test ;
21
21
22
+ import org .mockito .Mockito ;
22
23
import org .springframework .batch .core .ExitStatus ;
23
24
import org .springframework .batch .core .job .Job ;
24
25
import org .springframework .batch .core .job .JobExecution ;
40
41
import org .springframework .transaction .PlatformTransactionManager ;
41
42
42
43
import static org .junit .jupiter .api .Assertions .assertEquals ;
44
+ import static org .junit .jupiter .api .Assertions .assertThrows ;
43
45
44
46
/**
45
47
* @author Mahmoud Ben Hassine
@@ -65,6 +67,16 @@ void testListeners() throws Exception {
65
67
66
68
}
67
69
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
+
68
80
@ Configuration
69
81
@ EnableBatchProcessing
70
82
static class MyJobConfiguration {
@@ -130,4 +142,14 @@ public void afterJob(JobExecution jobExecution) {
130
142
131
143
}
132
144
145
+ public static class InvalidListener {
146
+
147
+ public void beforeStep () {
148
+ }
149
+
150
+ public void afterStep () {
151
+ }
152
+
153
+ }
154
+
133
155
}
Original file line number Diff line number Diff line change 60
60
import org .springframework .transaction .PlatformTransactionManager ;
61
61
62
62
import static org .junit .jupiter .api .Assertions .assertEquals ;
63
+ import static org .junit .jupiter .api .Assertions .assertThrows ;
63
64
64
65
/**
65
66
* @author Dave Syer
@@ -117,6 +118,13 @@ void testListeners() throws Exception {
117
118
assertEquals (1 , AnnotationBasedStepExecutionListener .afterChunkCount );
118
119
}
119
120
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
+
120
128
@ Test
121
129
void testAnnotationBasedChunkListenerForTaskletStep () throws Exception {
122
130
TaskletStepBuilder builder = new StepBuilder ("step" , jobRepository )
@@ -465,4 +473,14 @@ public void afterChunkError() {
465
473
466
474
}
467
475
476
+ public static class InvalidListener {
477
+
478
+ public void beforeStep () {
479
+ }
480
+
481
+ public void afterStep () {
482
+ }
483
+
484
+ }
485
+
468
486
}
You can’t perform that action at this time.
0 commit comments