Skip to content

Commit 21ea4d4

Browse files
committed
Improve error messages in JobParametersBuilder
Resolves #4581
1 parent a1ce07e commit 21ea4d4

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

spring-batch-core/src/main/java/org/springframework/batch/core/JobParametersBuilder.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ public JobParametersBuilder addString(String key, @NonNull String parameter) {
105105
* @return a reference to this object.
106106
*/
107107
public JobParametersBuilder addString(String key, @NonNull String parameter, boolean identifying) {
108+
Assert.notNull(parameter, "Value for parameter '" + key + "' must not be null");
108109
this.parameterMap.put(key, new JobParameter<>(parameter, String.class, identifying));
109110
return this;
110111
}
@@ -128,6 +129,7 @@ public JobParametersBuilder addDate(String key, @NonNull Date parameter) {
128129
* @return a reference to this object.
129130
*/
130131
public JobParametersBuilder addDate(String key, @NonNull Date parameter, boolean identifying) {
132+
Assert.notNull(parameter, "Value for parameter '" + key + "' must not be null");
131133
this.parameterMap.put(key, new JobParameter<>(parameter, Date.class, identifying));
132134
return this;
133135
}
@@ -151,6 +153,7 @@ public JobParametersBuilder addLocalDate(String key, @NonNull LocalDate paramete
151153
* @return a reference to this object.
152154
*/
153155
public JobParametersBuilder addLocalDate(String key, @NonNull LocalDate parameter, boolean identifying) {
156+
Assert.notNull(parameter, "Value for parameter '" + key + "' must not be null");
154157
this.parameterMap.put(key, new JobParameter<>(parameter, LocalDate.class, identifying));
155158
return this;
156159
}
@@ -174,6 +177,7 @@ public JobParametersBuilder addLocalTime(String key, @NonNull LocalTime paramete
174177
* @return a reference to this object.
175178
*/
176179
public JobParametersBuilder addLocalTime(String key, @NonNull LocalTime parameter, boolean identifying) {
180+
Assert.notNull(parameter, "Value for parameter '" + key + "' must not be null");
177181
this.parameterMap.put(key, new JobParameter<>(parameter, LocalTime.class, identifying));
178182
return this;
179183
}
@@ -197,6 +201,7 @@ public JobParametersBuilder addLocalDateTime(String key, @NonNull LocalDateTime
197201
* @return a reference to this object.
198202
*/
199203
public JobParametersBuilder addLocalDateTime(String key, @NonNull LocalDateTime parameter, boolean identifying) {
204+
Assert.notNull(parameter, "Value for parameter '" + key + "' must not be null");
200205
this.parameterMap.put(key, new JobParameter<>(parameter, LocalDateTime.class, identifying));
201206
return this;
202207
}
@@ -220,6 +225,7 @@ public JobParametersBuilder addLong(String key, @NonNull Long parameter) {
220225
* @return a reference to this object.
221226
*/
222227
public JobParametersBuilder addLong(String key, @NonNull Long parameter, boolean identifying) {
228+
Assert.notNull(parameter, "Value for parameter '" + key + "' must not be null");
223229
this.parameterMap.put(key, new JobParameter<>(parameter, Long.class, identifying));
224230
return this;
225231
}
@@ -243,6 +249,7 @@ public JobParametersBuilder addDouble(String key, @NonNull Double parameter) {
243249
* @return a reference to this object.
244250
*/
245251
public JobParametersBuilder addDouble(String key, @NonNull Double parameter, boolean identifying) {
252+
Assert.notNull(parameter, "Value for parameter '" + key + "' must not be null");
246253
this.parameterMap.put(key, new JobParameter<>(parameter, Double.class, identifying));
247254
return this;
248255
}
@@ -285,27 +292,28 @@ public JobParametersBuilder addJobParameter(String key, JobParameter<?> jobParam
285292
/**
286293
* Add a job parameter.
287294
* @param name the name of the parameter
288-
* @param value the value of the parameter
295+
* @param value the value of the parameter. Must not be {@code null}.
289296
* @param type the type of the parameter
290297
* @param identifying true if the parameter is identifying. false otherwise
291298
* @return a reference to this object.
292299
* @param <T> the type of the parameter
293300
* @since 5.0
294301
*/
295-
public <T> JobParametersBuilder addJobParameter(String name, T value, Class<T> type, boolean identifying) {
302+
public <T> JobParametersBuilder addJobParameter(String name, @NonNull T value, Class<T> type, boolean identifying) {
303+
Assert.notNull(value, "Value for parameter '" + name + "' must not be null");
296304
return addJobParameter(name, new JobParameter<>(value, type, identifying));
297305
}
298306

299307
/**
300308
* Add an identifying job parameter.
301309
* @param name the name of the parameter
302-
* @param value the value of the parameter
310+
* @param value the value of the parameter. Must not be {@code null}.
303311
* @param type the type of the parameter
304312
* @return a reference to this object.
305313
* @param <T> the type of the parameter
306314
* @since 5.0
307315
*/
308-
public <T> JobParametersBuilder addJobParameter(String name, T value, Class<T> type) {
316+
public <T> JobParametersBuilder addJobParameter(String name, @NonNull T value, Class<T> type) {
309317
return addJobParameter(name, value, type, true);
310318
}
311319

spring-batch-core/src/test/java/org/springframework/batch/core/JobParametersBuilderTests.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2008-2023 the original author or authors.
2+
* Copyright 2008-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@
2222
import java.util.Map;
2323
import java.util.Set;
2424

25+
import org.junit.jupiter.api.Assertions;
2526
import org.junit.jupiter.api.BeforeEach;
2627
import org.junit.jupiter.api.Test;
2728

@@ -85,6 +86,13 @@ void testAddingExistingJobParameters() {
8586
assertEquals(finalParams.getString("baz"), "quix");
8687
}
8788

89+
@Test
90+
void testAddingNullJobParameters() {
91+
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class,
92+
() -> new JobParametersBuilder().addString("foo", null).toJobParameters());
93+
Assertions.assertEquals("Value for parameter 'foo' must not be null", exception.getMessage());
94+
}
95+
8896
@Test
8997
void testNonIdentifyingParameters() {
9098
this.parametersBuilder.addDate("SCHEDULE_DATE", date, false);

0 commit comments

Comments
 (0)