Skip to content

Commit debb30b

Browse files
committed
Add unit test for the ExecutionContext.get method added in Issue spring-projects#718
1 parent 53e33c0 commit debb30b

File tree

1 file changed

+55
-7
lines changed

1 file changed

+55
-7
lines changed

spring-batch-infrastructure/src/test/java/org/springframework/batch/item/ExecutionContextTests.java

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@
1515
*/
1616
package org.springframework.batch.item;
1717

18-
import static org.junit.jupiter.api.Assertions.assertEquals;
19-
import static org.junit.jupiter.api.Assertions.assertFalse;
20-
import static org.junit.jupiter.api.Assertions.assertNotEquals;
21-
import static org.junit.jupiter.api.Assertions.assertNull;
22-
import static org.junit.jupiter.api.Assertions.assertThrows;
23-
import static org.junit.jupiter.api.Assertions.assertTrue;
24-
2518
import java.io.Serializable;
19+
import java.util.ArrayList;
20+
import java.util.List;
2621

22+
import com.google.gson.reflect.TypeToken;
23+
import org.assertj.core.api.Assertions;
24+
import org.junit.jupiter.api.DisplayName;
2725
import org.junit.jupiter.api.Test;
2826
import org.springframework.util.SerializationUtils;
2927

28+
import static org.junit.jupiter.api.Assertions.*;
29+
3030
/**
3131
* @author Lucas Ward
3232
* @author Mahmoud Ben Hassine
@@ -196,4 +196,52 @@ public boolean equals(Object obj) {
196196

197197
}
198198

199+
@DisplayName("testGetByType")
200+
@Test
201+
void givenAList_whenGettingAccordingToListType_thenReturnCorrectObject() {
202+
// given - a list
203+
String key = "aListObject";
204+
List<String> value = List.of("value1", "value2");
205+
context.put(key, value);
206+
// when - getting according to list type
207+
@SuppressWarnings("unchecked")
208+
List<String> result = (List<String>) context.get(key, List.class);
209+
// then - return
210+
assertEquals(result, value);
211+
assertEquals(result.get(0), value.get(0));
212+
assertEquals(result.get(1), value.get(1));
213+
}
214+
215+
@DisplayName("testGetNullByDefaultParam")
216+
@Test
217+
void givenAnNullList_whenGettingTheNullList_thenReturnNull() {
218+
// given - a null list
219+
String key = "aListObjectButNull";
220+
List<String> value = null;
221+
context.put(key, value);
222+
// when - getting according to the key
223+
@SuppressWarnings("unchecked")
224+
List<String> result = (List<String>) context.get(key, List.class, null);
225+
// then - return the defined null list
226+
assertNull(result);
227+
}
228+
229+
@DisplayName("testGetNullByNotNullDefaultParam")
230+
@Test
231+
void givenAnNullList_whenGettingNullWithNonNullDefault_thenReturnDefinedDefaultValue() {
232+
// given - a null list and expected default value
233+
String key = "aListObjectButNull";
234+
List<String> value = null;
235+
List<String> defaultValue = new ArrayList<>();
236+
defaultValue.add("value1");
237+
context.put(key, value);
238+
@SuppressWarnings("unchecked")
239+
// when - getting according to list type and default value
240+
List<String> result = (List<String>) context.get(key, List.class, defaultValue);
241+
// then - return defined default value
242+
assertNotNull(result);
243+
assertEquals(result, defaultValue);
244+
assertEquals(result.get(0), defaultValue.get(0));
245+
}
246+
199247
}

0 commit comments

Comments
 (0)