Skip to content

Commit b020f41

Browse files
committed
Improve error messages in JobParametersBuilder
Resolves #4581
1 parent 936cb11 commit b020f41

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
}
@@ -271,27 +278,28 @@ public JobParametersBuilder addJobParameter(String key, JobParameter<?> jobParam
271278
/**
272279
* Add a job parameter.
273280
* @param name the name of the parameter
274-
* @param value the value of the parameter
281+
* @param value the value of the parameter. Must not be {@code null}.
275282
* @param type the type of the parameter
276283
* @param identifying true if the parameter is identifying. false otherwise
277284
* @return a reference to this object.
278285
* @param <T> the type of the parameter
279286
* @since 5.0
280287
*/
281-
public <T> JobParametersBuilder addJobParameter(String name, T value, Class<T> type, boolean identifying) {
288+
public <T> JobParametersBuilder addJobParameter(String name, @NonNull T value, Class<T> type, boolean identifying) {
289+
Assert.notNull(value, "Value for parameter '" + name + "' must not be null");
282290
return addJobParameter(name, new JobParameter<>(value, type, identifying));
283291
}
284292

285293
/**
286294
* Add an identifying job parameter.
287295
* @param name the name of the parameter
288-
* @param value the value of the parameter
296+
* @param value the value of the parameter. Must not be {@code null}.
289297
* @param type the type of the parameter
290298
* @return a reference to this object.
291299
* @param <T> the type of the parameter
292300
* @since 5.0
293301
*/
294-
public <T> JobParametersBuilder addJobParameter(String name, T value, Class<T> type) {
302+
public <T> JobParametersBuilder addJobParameter(String name, @NonNull T value, Class<T> type) {
295303
return addJobParameter(name, value, type, true);
296304
}
297305

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)